need to compare the values in two arrays

hoeoreoweor
Kilo Explorer

Hi there, looking for a bit of help with a script include.

I have two arrays

array1 and array2

array1 contains a list of reserved items (item1, item6, item8, item15)

array2 contains all items (item1,item2, item3, item4, item5, item6, item7, item8, item9, item10, etc ----> item20)

Basically I need to subtract array1 from array2 - the goal is to only show 'Unreserved available items'.

I've previously removed duplicate values from a single array, but not one array from another...

Appreciate that I need to iterate through array1 and compare values at each position in array2, then add unique values to an 'array3', but struggling to get anything to work...

Any suggestions would be much appreciated.

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Please try something like that, just check if the array2 value is there in array1 then don't include that in array3, please check below code in your background script.



var array1 = ['item1', 'item6', 'item8', 'item15'];


var array2 = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10'];


var array3 = [];


for(var i = 0; i < array2.length; i++){


if(array1.indexOf(array2[i]) == -1){


array3.push(array2[i]);


}


}


gs.print(array3);


View solution in original post

3 REPLIES 3

Shishir Srivast
Mega Sage

Please try something like that, just check if the array2 value is there in array1 then don't include that in array3, please check below code in your background script.



var array1 = ['item1', 'item6', 'item8', 'item15'];


var array2 = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10'];


var array3 = [];


for(var i = 0; i < array2.length; i++){


if(array1.indexOf(array2[i]) == -1){


array3.push(array2[i]);


}


}


gs.print(array3);


sachin_namjoshi
Kilo Patron
Kilo Patron

The following solution proposed by ctomasi should be of help.



Glide List Differ



Also... please note the comment on that post which suggests to use diff, intersect, and union when using the ArrayUtil.



Regards,


Sachin


hoeoreoweor
Kilo Explorer

Thanks to all for your help on this. Much appreciated!