- 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
01-29-2024 04:03 AM
Hi @k_47
Have you tried using Object.keys(name of your array).length to get the length ? Refer https://linuxhint.com/dictionary-length-javascript/
Thanks & Regards
Amit Verma
Thanks & Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- 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
01-29-2024 10:59 AM
yes, assign values to any variable and use length method
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 09:54 PM
Hi,
Were you able to get a solution for this? I also have the same issue.