How to process array of objects to get specific value ??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 06:07 AM
I am having the worknotes as shown below.
[ {"Name":"Mahesh","Response":"Approve","Comments":"SRR0074637, Rejected!"},
{"Name":"Gopi","Response":"Reject","Comments":"SRR0074637, Rejected!"} ]
I need to loop in to each object and whenever i found reject as a response i need to return corresponding comments from the above array.
how i need to do through script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 06:13 AM
try this in background script
var worknotes = [
{"Name": "Mahesh", "Response": "Approve", "Comments": "SRR0074637, Rejected!"},
{"Name": "Gopi", "Response": "Reject", "Comments": "SRR0074637, Rejected!"}
];
function getRejectComments(worknotes) {
for (var i = 0; i < worknotes.length; i++) {
if (worknotes[i].Response === "Reject") {
return worknotes[i].Comments;
}
}
return null; // Return null if no reject response is found
}
var rejectComments = getRejectComments(worknotes);
gs.info('Reject Comments: ' + rejectComments);
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 07:40 AM
Thank you for marking my response as helpful.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 06:20 AM - edited 03-27-2025 06:23 AM
Hi @KM SN
You can use below code:
var workNotes = [
{ "Name": "Mahesh", "Response": "Approve", "Comments": "SRRO074637, Rejected." },
{ "Name": "Gopi", "Response": "Reject", "Comments": "SRR0074637, Rejected!" }
];
// Loop through the array
workNotes.forEach(entry => {
// Check if the response is "Reject"
if (entry.Response === "Reject") {
// Print the corresponding comment
gs.info(entry.Comments);
}
});
If my response helped, please hit the Thumb Icon and accept the solution so that it benefits future readers.
Regards,
Rohit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 07:23 AM
Hi @KM SN ,
Please accept the solution as well and close the thread so that it benefit future readers as well.
Regards,
Rohit