In a bit of a departure from my regularly scheduled posts on underused features, we’ll talk about something that’s often a mystery to my customers when I first mention it: Dot-walking.
This may be more of an explanation than you wanted, but here I’ll answer this question: What’s up with all those ‘.’s?
We have to start with the basics. In programming, there are some object types that are pretty fundamental. Strings are one: “The quick brown fox jumps over the lazy dog.” The next object type to which we’re usually introduced is arrays. With an array, each element must be numbered. This is nice because you can always access the elements in a sequential order and you can sort them in a particular order, making ordered loops possible. Arrays are nice because they hold multiple values in a single variable, but it can be very annoying to work with them.
For example, if you want to access a specific item in an array, you have to know its numerical place. If you’re anything like me, you have trouble remembering a phone number, let alone how many elements you have in your various arrays!
So in this post, we’ll come up with an easier way to deal with this conundrum.
In Javascript, objects (sometimes called hash maps) are essentially arrays (and yes, completely different, too) with elements that are “named” instead of numbered.
Need to sort a list of objects alphabetically? No problem!
(Note: all code samples run in “Scripts – Background,” which may require the Security Admin role.)
var someNamesArr = ["Bob", "Warner", "Susan", "Clive"];
someNamesArr.sort();
gs.log(someNamesArr.join(", ")); // logs: Bob, Clive, Susan, Warner
But let’s start our comparison of arrays and objects by looking at a list of math operators. First, in Array format:
Key | Value |
0 | + |
1 | - |
2 | = |
3 | * |
4 | / |
5 | ^ |
When you build it, you can build the array literally, assigning each operator above an element number, but don’t forget that arrays start at zero, not one.
var myArray = []; // the square brackets create a new, empty array
myArray[0] = "+";
myArray[1] = "-";
myArray[2] = "=";
myArray[3] = "*";
myArray[4] = "/";
myArray[5] = "^";
gs.log('Array location 5 is ' + myArray[5]); // prints "^"
When you know the specific item you want, you can access it by calling the number to which its assignedbut you have to know and remember the number.
Now, let’s see how this same example would work with objects. A “hash map,” or object, can be just a list of terms with values like what we had with arrays:
Key | Value |
add | + |
subtract | - |
equate | = |
multiply | * |
divide | / |
power | ^ |
So when you build a hash map, you can do this:
var myHash = {}; // "{}" creates a new generic Object
myHash.add = "+";
myHash.subtract = "-";
myHash.equate = "=";
myHash.multiply = "*";
myHash.divide = "/";
myHash.power = "^";
// You can now access the named property
gs.log('Power symbol is ' + myHash["power"]); // returns "^"
The format above is called bracket-notation, but you might notice that it is actually the same notation as an array uses, just with a string-accessible element instead of an integer-accessible element.
Furthermore, just like you set the value of the property, you can also access the value using a dot, called dot-notation:
gs.log('Power symbol is ' + myHash.power); // returns "^"
This is where we get all those dots in service now! Need the phone number for the manager of the current incident’s caller? No problem!
We’ll just access the properties (upon properties) of the current incident:
gs.log("Caller manager's phone: " + current.caller_id.manager.phone);
Now that we know how to use objects, let’s address the real power of arrays: Loops.
How do I loop through an object?
Loops
When you want to access the contents of a hash map or an array, you can use the for statement. First, here’s what we’re used to seeing in an array, which will loop over the contents in order:
for (var i=0; i < myArray.length; i++){
gs.log("Key is " + i + " and value is " + myArray[i]);
}
Now, if we don’t care about the order, we can do the following in an array (Note that there is no guarantee of order when you use this method):
for (var key in myArray){
gs.log("Key is " + key + " and value is " + myArray[key]);
}
Which results in something like:
*** Script: Key is 0 and value is +
*** Script: Key is 1 and value is -
*** Script: Key is 2 and value is =
*** Script: Key is 3 and value is *
*** Script: Key is 4 and value is /
*** Script: Key is 5 and value is ^
Finally, what about looping over an object?
for (var key in myHash){
gs.log("Key is " + key + " and value is " + myHash[key]);
}
The FOR operator looks up all the “keys” (you can name the variable whatever you like), and prints the value. The above results in:
*** Script: Key is divide and value is /
*** Script: Key is subtract and value is -
*** Script: Key is equals and value is =
*** Script: Key is power and value is ^
*** Script: Key is multiply and value is *
*** Script: Key is addition and value is +
Now, we have a friendly way to access the properties of our object–in a way that we can remember and understand as we read. Simply type “myHash.add”, “myHash.subtract”, etc. instead of using numbered properties.
The post ServiceNow Tech Tuesdays: JavaScript Objects and Dot-Walking appeared first on Cloud Sherpas.