Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to process array of objects to get specific value ??

KM SN
Tera Expert

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?

5 REPLIES 5

Robert H
Mega Sage

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