- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
12-08-2024 11:33 PM - edited 12-08-2024 11:34 PM
Hello Community,
Arrays are one of the most commonly used data structures in JavaScript, and they are also prevalent in ServiceNow scripting. They provide a way to store multiple values in a single variable, making it easier to handle and manipulate data sets.
This article dives into some of the array methods used in ServiceNow.
Removing Duplicates from an Array
We can remove duplicates from an array in ServiceNow using the filter() method.
var names = ["Pranav", "Chuk", "Pranav", "Fred", "Abel"];
var uniqueNames = names.filter(function(item, index) {
return names.indexOf(item) === index;
});
gs.info(uniqueNames); // Output: ["Pranav", "Chuk", "Fred", "Abel"]
Sorting an Array
We can sort an array in ascending or descending order using the sort() method.
var numbers = [34, 7, 23, 32, 5, 62];
numbers.sort(function(a, b) {
return a - b; // Ascending
});
gs.info(numbers); // Output: [5, 7, 23, 32, 34, 62]
numbers.sort(function(a, b) {
return b - a; // Descending
});
gs.info(numbers); // Output: [62, 34, 32, 23, 7, 5]
Note:
- By default, the sort() method sorts elements as strings (e.g., ["10", "2", "34"] → ["10", "2", "34"]). Using a custom compare function ensures numerical sorting.
- The sorting modifies the original numbers array (it’s not a new array).
Filtering an Array Based on a Condition
Filter out elements based on a specific condition.
var numbers = [10, 25, 30, 45, 60];
var filteredNumbers = numbers.filter(function(num) {
return num > 30; // Only numbers greater than 30
});
gs.info(filteredNumbers); // Output: [45, 60]
Finding the Maximum and Minimum Values in an Array
We can use the Math.max() and Math.min() functions.
var numbers = [10, 25, 30, 45, 60];
var max = Math.max.apply(null, numbers);
var min = Math.min.apply(null, numbers);
gs.info("Max: " + max); // Output: Max: 60
gs.info("Min: " + min); // Output: Min: 10
- Math.max() and Math.min() don't natively work with arrays. The .apply() method lets you call these functions with an array of arguments.
- The first parameter of apply() is the this context (irrelevant for static methods like Math.max or Math.min). You can pass null or undefined.
- If working with large arrays, using .apply() might have performance limitations due to the spread of arguments. Alternatively, we can use the Modern ES6 spread syntax (… operator) e.g.:
- var max = Math.max(...numbers);
Reducing an Array to a Single Value
Use the reduce() method to compute a single value, like a sum, product etc.
var numbers = [10, 20, 30];
var sum = numbers.reduce(function(total, current) {
return total + current;
});
gs.info("Sum: " + sum); // Output: Sum: 60
Splitting and Joining Arrays
We can split a string into an array and join an array back into a string.
var csv = "apple,banana,grape";
var fruitsArray = csv.split(",");
gs.info(fruitsArray); // Output: ["apple", "banana", "grape"]
var fruitsString = fruitsArray.join(" | ");
gs.info(fruitsString); // Output: "apple | banana | grape"
Removing Specific Elements
Use splice() to remove specific elements.
var numbers = [10, 20, 30, 40, 50];
numbers.splice(1, 2); // Remove 2 elements from index 1
gs.info(numbers ); // Output: [10, 40, 50]
Adding and Removing element at start of the array
We can use shift() to remove element at start of the array and unshift() to add element at start of the array
var numbers = [10, 20, 30, 40, 50];
numbers.shift();
gs.info(numbers); // output: 20,30,40,50
numbers.unshift(5);
gs.info(numbers); // output: 5,20,30,40,50
Note: we have push() and pop() methods also, push() adds an element at the end of the array and pop() removes an element at the end of the array.
There are so many methods in JavaScript for array manipulation. Some of them support Modern ES6 only.
Please mark as Helpful, if this article found good and helpful
Thank you very much
Ramana Murthy G
- 2,994 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello Community,
I am new to this whole ServiceNow thing and I think I am having some trouble in the Scripting Part. In Scripts-Backdground, when I use Push() to add elements to an array, it replaces all the existing elements with the element being pushed into the array. so, anyone can help, I'll appreciate it.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello @KeerthiVasG ,
If you can paste your script or screen shot what you were written, people may be help you.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank You @RAMANA MURTHY G . But, I already found the help I needed from some other community posts. I just made the mistake of not converting the values to string before pushing into the array.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank You Ramana Murthy for posting the Array methods which is very helpful info. I am new to scripting and ServiceNow. Can you please also help us posting with some Real Time Scenarios on Change, Problem, Incident management and how it works. Thanks
Regards....Pavan