View Full Version : subtract elements in arrays
drawmedia
Dec 5th 2006, 10:54 am
Hello. I need your help here..
I have two arrays, like:
array1: 1,2,3,4
and
array2: 2,4
and I need to subtract values from the array1, that are present in the array2, so in the end i get array3: 1,3. I don't know the order of the values.. they can be like 5 3 1 2..
Any ideas or tips on how I can do this?
Thank you
Logic Ali
Dec 5th 2006, 2:26 pm
Hello. I need your help here..
I have two arrays, like:
array1: 1,2,3,4
and
array2: 2,4
and I need to subtract values from the array1, that are present in the array2, so in the end i get array3: 1,3. I don't know the order of the values.. they can be like 5 3 1 2..
Any ideas or tips on how I can do this?
Thank youYou need a nested for loop to compare the elements of the two arrays. When a match is found, use
Array.splice (http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:splice) to remove the matching elements. Simple.
weknowtheworld
Dec 23rd 2006, 12:25 am
u have to check the array values in a for loop and set the array value to NULL.. whereever same values are there..
Logic Ali
Dec 23rd 2006, 7:08 pm
u have to check the array values in a for loop and set the array value to NULL.. whereever same values are there..
Doing that would produce 1,,3,
Setting an element to null does not remove it from the array.
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.