Need suggestion on using For & foreach loop

shaik_irfan
Tera Guru

Hi,

I am trying to get some data from 3rd party system and parsing that data to get the id of users.

 

var str = responseBody;
var parser = new JSONParser();
var parsedData = parser.parse(str).members; // LIST of users from 3rd users


arrayList = [shaik.irfan, robert.ramirez, anthony.gonzalvez];


var finalObj = [];
for(var k=0; k<arrayList.length; k++)
	{
	parsedData.forEach(function(element) {
		if(element.name === arrayList[k]) {
			finalObj.push(element.id);
		}
	});
}

 

what i am doing is from arrayList i am searching with each user id from the parsedData if i get any user i am adding it into finalObj array.

 

Now i want to optmize the code to not use for loop inside forEach loop again since i am getting a warning message "Don't make functions within a loop" i want to seperate above for loops 

1 ACCEPTED SOLUTION

var str = '{"ok":true,"members"::[{"id":"ABCED", "name" : "shaik.irfan", "email" : "abce@abc.com"},{"id":"ABCED", "name" : "anthoy.ramirez", "email" : "abce@abc.com"},{"id":"ABCED", "name" : "fernando", "email" : "abce@abc.com"},]}';
var parser = new JSONParser();
var parsedData = parser.parse(str).members; // LIST of member objects
gs.info(parsedData.length);
var members = [];
for(var member=0; member<parsedData.length; member++)
{
members.push(parsedData[member].name); //stores an array strings
}
gs.info(members);

//members is an array of strings

var arrayUtil = new ArrayUtil();
var arrayList = ["shaik.irfan", "robert.ramirez", "anthony.gonzalvez"]; //your keys.make sure these are strings
gs.info(arrayList );
gs.info(arrayUtil.intersect(members, arrayList));

*** Script: 3
*** Script: shaik.irfan,anthoy.ramirez,fernando
*** Script: shaik.irfan,robert.ramirez,anthony.gonzalvez
*** Script: shaik.irfan
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shaik,

so basically you need to push value into an array from the json only for those keys; so use this code

you are already iterating the arrayList array and then again comparing if the key name is the one from array;

why not directly get values for those key and push into finalObj array

https://davidtang.io/2016/07/30/javascript-for-loop-vs-array-foreach.html

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

arrayList values i am getting from Servicenow by Gliding the User table which i am placing in the arrayList later form the parsedData i am searching if i get any record with the arrayList user id and pushing into another array. Can you please help me construct the code

Hi Shaik,

so you are forming the arrayList array first and then you will only fetch values from the json corresponding to those keys?

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yes. You are right ?