Multidimensional JavaScript array that needs pruning.
I have a multidimensional arary :
myArray =[[1, 23, 'Text', 'Text'],
[2, 389, 'Text', 'Text'],
[3, 42, 'Text', 'Text'],
[4, 2, 'Text', 'Text'],
[5, 17, 'Text', 'Text'],
[6, 45, 'Text', 'Text'],
[7, 95, 'Text', 'Text'],
[8, 101, 'Text', 'Text'],
[9, 112, 'Text', 'Text'],
[10, 902, 'Text', 'Text'],
[11, 9, 'Text', 'Text'],
[12, 22, 'Text', 'Text']];
I also have a single-dimensional array.
myOtherArary = [ 22, 45, 2, 11 ];
I wish to delete 'rows' from myArray where the second values i.e. (elements myArray[0][1], myArray[1][1], myArray[1][1].....myArray[11][1]) are not in myOtherArray.
For the sample values provided the final contents of myArray should be:
myArray =[[4, 2, 'Text1', 'Text2'],
[6, 45, 'Text1', 'Text2'],
[12, 22, 'Text1', 'Text2']];
How can I achieve this? Thanks in advance.
posted by kenaman to computers & internet (13 comments total)
var tempArray;
var found;
for (i = 0; i < myArray.length; i++)
{
element = myArray[i][1];
found = false;
for (j = 0; j < myOtherArray.length; j++)
{
if (element = myOtherArray[j])
{
found = true;
break;
}
if (found) tempArray.push(myArray[i]);
}
myArray = tempArray.slice();
}
posted by grumblebee at 9:08 AM on August 15, 2006