- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 01:18 PM
Hi,
I want to store sysid and an attribute value in a Map and want to store only unique sys id. I want to use this Map for firther processing in later part of hte code. How do I build and parse the map.
Any help is appreciated
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 04:40 PM - edited 01-30-2024 04:42 PM
How you build the map depends on multiple things, for example what is the structure of your data originally that you want to put into the map?
When talking about a "Map" in JavaScript, there's typically two data-structures that can be used for that. The first is an object `{}`, and the other is an ES6 Map. Both can't have duplicated keys. At present, ServiceNow only supports ES6 Maps if you're using a scoped-application with JavaScript Mode set to "ECMAScript 2021 (ES12)" (the default mode for scoped-apps created in Tokyo upwards).
Using an ES6 Map as a map
There are two main ways to add data to an ES6 Map, either via its constructor, or later using the `set()` method.
Constructor:
// ES6 Map example
// Pass the constructor an array of key-value pairs for the Map in the form of `[[key1, value1], [key2, value2], ...]`
const map = new Map([["sys_id1", "attribute1"], ["sys_id2", "attribute2"]]);
// Later you can read the values from the map usng `.get()`
gs.info("Attribute value for key sys_id1: " + map.get("sys_id1"));
// Logs:
// Attribute value for key sys_id1: attribute1
Using `.set()`:
// ES6 Map example
const map = new Map(); // empty map
map.set("sys_id1", "attribute1");
map.set("sys_id2", "attribute2");
// Later you can read the values from the map usng `.get()`
gs.info("Attribute value for key sys_id1: " + map.get("sys_id1"));
// Logs:
// Attribute value for key sys_id1: attribute1
Using an object literal as a map
If your script is in the global scope, or in a scoped app with the JavaScript mode set to something that doesn't support ES6, then you can use an object literal to represent your map. If you know the keys in advance, then you can add them directly:
// Using an object literal (for support in non-ES6 scripts)
var map = {
"sys_id1": "attribute_1",
"sys_id2": "attribute_2"
};
gs.info("Attribute value for key sys_id1: " + map["sys_id1"]);
// Logs:
// Attribute value for key sys_id1: attribute_1
If you need to dynamically add your keys, which is most likely your case, then can use bracket notation to add keys stored in variables (useful if you're getting your sys_ids from a loop for example):
// Using an object literal (for support in non-ES6 scripts)
var map = {}; // empty object
map["sys_id1"] = "attribute_1";
var exampleId = "sys_id2"; // if the id is in a variable...
map[exampleId] = "attribute_2"; // you can use bracket-notation to set the key name to variable's value
gs.info("Attribute value for key " + exampleId + ": " + map[exampleId]); // use bracket notation to access the value for the key stored in the `exampleId` variable
// Logs:
// Attribute value for key sys_id2: attribute_2
Why use a Map over an object literal?
If you can support the Map implementation then you should use that. Unlike objects which support only string and Symbol keys, Maps can store any data type as their keys, including objects. The other advantage of using Maps is that if you iterate the keys in the Map, it will iterate in the order in which you inserted them. Objects don't have this property, and while iteration order for objects is standardised in ES2021, it is not intuitive like it is for Maps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 04:40 PM - edited 01-30-2024 04:42 PM
How you build the map depends on multiple things, for example what is the structure of your data originally that you want to put into the map?
When talking about a "Map" in JavaScript, there's typically two data-structures that can be used for that. The first is an object `{}`, and the other is an ES6 Map. Both can't have duplicated keys. At present, ServiceNow only supports ES6 Maps if you're using a scoped-application with JavaScript Mode set to "ECMAScript 2021 (ES12)" (the default mode for scoped-apps created in Tokyo upwards).
Using an ES6 Map as a map
There are two main ways to add data to an ES6 Map, either via its constructor, or later using the `set()` method.
Constructor:
// ES6 Map example
// Pass the constructor an array of key-value pairs for the Map in the form of `[[key1, value1], [key2, value2], ...]`
const map = new Map([["sys_id1", "attribute1"], ["sys_id2", "attribute2"]]);
// Later you can read the values from the map usng `.get()`
gs.info("Attribute value for key sys_id1: " + map.get("sys_id1"));
// Logs:
// Attribute value for key sys_id1: attribute1
Using `.set()`:
// ES6 Map example
const map = new Map(); // empty map
map.set("sys_id1", "attribute1");
map.set("sys_id2", "attribute2");
// Later you can read the values from the map usng `.get()`
gs.info("Attribute value for key sys_id1: " + map.get("sys_id1"));
// Logs:
// Attribute value for key sys_id1: attribute1
Using an object literal as a map
If your script is in the global scope, or in a scoped app with the JavaScript mode set to something that doesn't support ES6, then you can use an object literal to represent your map. If you know the keys in advance, then you can add them directly:
// Using an object literal (for support in non-ES6 scripts)
var map = {
"sys_id1": "attribute_1",
"sys_id2": "attribute_2"
};
gs.info("Attribute value for key sys_id1: " + map["sys_id1"]);
// Logs:
// Attribute value for key sys_id1: attribute_1
If you need to dynamically add your keys, which is most likely your case, then can use bracket notation to add keys stored in variables (useful if you're getting your sys_ids from a loop for example):
// Using an object literal (for support in non-ES6 scripts)
var map = {}; // empty object
map["sys_id1"] = "attribute_1";
var exampleId = "sys_id2"; // if the id is in a variable...
map[exampleId] = "attribute_2"; // you can use bracket-notation to set the key name to variable's value
gs.info("Attribute value for key " + exampleId + ": " + map[exampleId]); // use bracket notation to access the value for the key stored in the `exampleId` variable
// Logs:
// Attribute value for key sys_id2: attribute_2
Why use a Map over an object literal?
If you can support the Map implementation then you should use that. Unlike objects which support only string and Symbol keys, Maps can store any data type as their keys, including objects. The other advantage of using Maps is that if you iterate the keys in the Map, it will iterate in the order in which you inserted them. Objects don't have this property, and while iteration order for objects is standardised in ES2021, it is not intuitive like it is for Maps.