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:26 AM - edited 03-27-2025 06:48 AM
Hello @KM SN ,
If you need just one random rejection comment then the solution provided by Ankur will work.
If multiple people can reject and you need all their comments then use something like this one-liner. It will create an array of all rejection comments, or an empty array if nobody rejected.
var workNotes = [{
"Name": "Mahesh",
"Response": "Approve",
"Comments": "SRR0074637, Approved!"
},
{
"Name": "Gopi",
"Response": "Reject",
"Comments": "SRR0074637, Rejected!"
},
{
"Name": "SomeoneElse",
"Response": "Reject",
"Comments": "SRR0074637, I am also rejecting"
}
];
gs.info(workNotes.filter(note => note.Response === 'Reject').map(note => note.Comments));
Output:
SRR0074637, Rejected!,SRR0074637, I am also rejecting
Regards,
Robert