Pruning JavaScript array.
August 15, 2006 8:34 AM   Subscribe

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 answers total)
 
var element;
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


As Grumblebee's example illustrates, you don't remove the rows from the array, but rather you build a new array with the rows you want.
posted by utsutsu at 9:16 AM on August 15, 2006


utsutsu writes "you don't remove the rows from the array, but rather you build a new array with the rows you want."


It's kinda the opposite of Rumsfeld: You don't go to code with the arrays you have, you go to code with the arrays you want.
posted by orthogonality at 9:24 AM on August 15, 2006


function prune_rows(array, condition) {
	var result = [];
	for (var i = 0; i < array.length; ++i) {br>
		if (!condition(array, i)) {
			result.push(array[i]);
		}
	}
	return result;
}
		
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']
];

function to_be_pruned(array, row_index) {
	var myOtherArray = [22, 45, 2, 11];
	return myOtherArray.indexOf(array[row_index][1]) == -1;
}

alert(prune_rows(myArray, to_be_pruned));

posted by koenie at 9:32 AM on August 15, 2006


koenie -

return myOtherArray.indexOf(array[row_index][1]) == -1;

throws error :Object doesnt support this property or method.

myOtherArray and arry both contain what one would expect them to and row_index = 0.

Any ideas?
posted by kenaman at 9:52 AM on August 15, 2006


koenie - googled it and see that it is not supported across all browsers. Will figure it out myself. Cheers. Anyway if anyone else wants to come up with a clever way to skin an array, please do.
posted by kenaman at 10:22 AM on August 15, 2006


function prune(a, deleteKeys) {
    var keys = ','+deleteKeys.join(',')+',';
    var len = a.length;
    for(var i=0;i
        if(keys.indexOf(','+a[i][1]+',') == -1)
            a.push(a[i]);
    a.splice(0,len);
}

		

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'],
	[10, 902, 'Text', 'Text'],
	[12, 22,  'Text', 'Text']
];


var delArray = [22, 45, 2, 11];

prune(myArray, delArray);


myArray is your new array.
posted by null terminated at 1:34 PM on August 15, 2006


That didn't work too well. Here's the prune function:

function prune(a, deleteKeys) {
var keys = ','+deleteKeys.join(',')+',';
var len = a.length;
for(var i=0;i<len;i++)
if(keys.indexOf(','+a[i][1]+',') == -1)
a.push(a[i]);
a.splice(0,len);
}
posted by null terminated at 1:37 PM on August 15, 2006


looks like &nbsp;s aren't being converted.
posted by null terminated at 1:38 PM on August 15, 2006


argh, my answer was backwards. This line:

if(keys.indexOf(','+a[i][1]+',') == -1)

should be

if(keys.indexOf(','+a[i][1]+',') != -1)
posted by null terminated at 1:45 PM on August 15, 2006


kenaman, did my method work for you?
posted by grumblebee at 2:58 PM on August 15, 2006


(We need to lobby Matt to give us a way to include code examples, complete with indenting and character parsing. I posted a MeTa thread about it, and Matt agreed it was a good idead, but I think it's on the back burner.)
posted by grumblebee at 3:03 PM on August 15, 2006


Thanks for the answers guys. All worked when adapted. Sorry for replying late but I am in eurpoe working late and had to go home. The help was appreciated.

grumblebee I second the need for facilitating code examples. Formatting is a nightmare.

Anyway thanks again.
posted by kenaman at 12:40 AM on August 16, 2006


« Older Philadelphia couples oriented sex shops?   |   Google eps? Newer »
This thread is closed to new comments.