javascript array headache

Giganews Newsgroups
Subject: javascript array headache
Posted by:  M D (…@md.com)
Date: Tue, 12 Sep 2006

How can make the following program so that
remove not matching fruits from a1 and add rest in the a2 to
a1.
answer
["Apple","Grape","Pear","Water Melon", "Straberry"];
currently i get the first 3 because concat won't alocate the new
memory space for a1[4] a1[5]

var a1 = ["Apple","Banana","Orange","Grape","Pear"];
var a2 = ["Grape","Pear","Apple","Water Melon", "Straberry"];
var found;

for( var j = 0; j < a1.length; j++ ){
found = "false";
for( var i = 0; i < a2.length; i++ ){
  if(  a1[ j ] == a2[i] ){
  found = "true";
  a2.splice(i,1);
  i--;
  break;
  }else{

  }
}

if( found == "false" ){
  a1.splice(j,1);
  j--;
}
}
alert( a1.length );
alert( a2.length );
a1.concat( a2 );
alert( a1.length );
for( var l = 0; l < a1.length ; l++ ){
alert( a1[l] );
}

Replies