
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2021 08:23 AM
Hello all,
I've got an array that I'm building through a GlideRecord query.
I want to see if all the values match.
For example, here is the array: [a,a,a,a,b]
Here is some javascript I found online:
var array1 = [a,a,a,a,b];
var check = array1.every( (val, i, arr) => val === arr[0] );
gs.info(check);
In the example above, the expected result would be 'false'.
However, when I run the above, I get Javascript compiler errors (line 2 specifically).
I'm sure there's another way to do this check, just not sure how.
Any help is greatly appreciated-
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2021 08:33 AM
You can't use every as it is ES6 format, ES5 is support in servicenow, try this
function identical(array) {
for(var i = 0; i < array.length - 1; i++) {
if(array[i] !== array[i+1]) {
return false;
}
}
return true;
}
Background script code
function identical(array) {
for(var i = 0; i < array.length - 1; i++) {
if(array[i] !== array[i+1]) {
return false;
}
}
return true;
}
var a = [a,a,a,a]
gs.print(identical(a));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2021 08:30 AM
Hi,
There are multiple ways to compare that. A quick way is to convert both the arrays to JSON string and then compare.
Sample script. [Try below in background script for a quick look]
var a = [1, 2, 3, 5];
var b = [1, 2, 4, 5];
// Comparing both arrays using stringify
if(JSON.stringify(a)==JSON.stringify(b)){
gs.info("same");
} else {
gs.info("Different");
}
Regards,
Muhammad
Muhammad

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2021 08:40 AM
Hi Muhammad,
I'm only 1 array, not 2. If there were 2 arrays, this would be cake!
Good suggestion though

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2021 08:42 AM
Oops I misread I believe. Please try Pranav's code below that will work!
Muhammad

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-02-2021 08:32 AM
Hi,
Do you want check check weather array has duplicate values?
Please check below link, you may find it helpful:
Thanks,
Anil Lande
Thanks
Anil Lande