- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2024 03:50 AM
I would like to get the length and value of the following array in Client Script.
[{"key1": "a", "key2": "c", "key3": "e"}
,{"key1": "b", "key2": "d", "key3": "f"}]
*a-f contain sys_id.
The results I want are as follows
Length: 2 (for {}*2)
Value: a-f
This array is from getting the value of Multi Rows in variable set.
I tried using foreach, JSON.stringify, JSON.parse, etc,
I could not get the value, perhaps I am using them incorrectly.
Is it not possible to use it for [{}]type arrays?
If you know anything about this, please let me know.
Best Regards
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2024 04:47 AM
Hello @k_47
Can you try below logic...
I have tried it in background script...you can try once...
/*1.Assuming this is data we get */
var payload = [{"key1": "a", "key2": "c", "key3": "e"}
,{"key1": "b", "key2": "d", "key3": "f"}];
/*2.Print length of payload array */
gs.print(payload.length);
/*3.Decalre array */
var myKeysValues =[];
/*4. Loop through payload array */
for(var i=0; i<payload.length ;i++){
/*4.1 Here we will get array of kays for objects in payload array */
var getKeys = Object.keys(payload[i])
/*4.2 Loop through getKeys array */
for(var j=0; j<getKeys.length;j++){
/*4.3 Push values of keys in myKeusValues array */
myKeysValues.push(payload[i][getKeys[j]]);
}
}
gs.print(myKeysValues);
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 07:47 PM
@hamlin, can you share your issue? possibly code?