- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2017 05:24 AM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2017 07:09 AM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2017 07:09 AM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2017 07:58 AM
The following solution proposed by ctomasi should be of help.
Also... please note the comment on that post which suggests to use diff, intersect, and union when using the ArrayUtil.
Regards,
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2017 08:44 AM
Thanks to all for your help on this. Much appreciated!