Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Kalaiarasan Pus
Giga Sage

The long-lost cousin of ArrayUtil which no one talks about is ArrayObjectUtil. ArrayObjectUtil is a script include with useful functions for working with objects.

Since I did not find any documentation for this script include, decided to write a blog documenting the functions it provides.

Below are some of the functions that I found useful.

 

extractUniq:  Extracts unique values from a object array key.

var employees = [
{'name' : 'John' , 'id': '111'},
{'name' : 'John' , 'id' : '100' },
{'name' : 'Joe' , 'id' : '123' },
{'name' : 'Sun' , 'id' : '110' },
{'name' : 'Park' , 'id' : '150' },
];
gs.info(JSON.stringify(ArrayObjectUtil.extractUniq(employees,'name')));

Output: ["John","Joe","Sun","Park"]

 

extractObjectByKey: Extracts first object that contains the given key.

var employees = [
{'id': '111'},
{'name' : 'John'},
{'email': 'john@gmail.com'},
];

gs.info(ArrayObjectUtil.extractObjectByKey(employees,'email'));

Output: john@gmail.com

 

filter: Returns array of objects that contains the specified key and value.

var employees = [
{'name' : 'John' , 'id': '111'},
{'name' : 'John' , 'id' : '100' },
{'name' : 'Joe' , 'id' : '123' },
{'name' : 'Sun' , 'id' : '110' },
{'name' : 'Park' , 'id' : '150' },
];
gs.info(JSON.stringify(ArrayObjectUtil.filter(employees,'name','John')));

Output: [{"name":"John","id":"111"},{"name":"John","id":"100"}]

 

filterFirst: Extracts the first object that contains the specified key and value.

var employees = [
{'name' : 'John' , 'id': '111'},
{'name' : 'John' , 'id' : '100' },
{'name' : 'Joe' , 'id' : '123' },
{'name' : 'Sun' , 'id' : '110' },
{'name' : 'Park' , 'id' : '150' },
];
gs.info(JSON.stringify(ArrayObjectUtil.filterFirst(employees,'name','John')));

Output: {"name":"John","id":"111"}

 

combineObjects: Merge two objects into one.

var employeeSet1 = {'name' : 'John' , 'id': '111'};
var employeeSet2 = {'email' : 'John@gmail.com' , 'id': '111'};
gs.info(JSON.stringify(ArrayObjectUtil.combineObjects(employeeSet1,employeeSet2)));

Output: {"name":"John","id":"111","email":"John@gmail.com"}

 

removeEmpty: Remove empty objects from the array of objects.

var employees = [
{'name' : 'John' , 'id': '111'},
{},
{'name' : 'Joe' , 'id' : '123' },
{'name' : 'Sun' , 'id' : '110' },
{'name' : 'Park' , 'id' : '150' },
];
gs.info(JSON.stringify(ArrayObjectUtil.removeEmpty(employees)));

Output: [{"name":"John","id":"111"},{"name":"Joe","id":"123"},{"name":"Sun","id":"110"},{"name":"Park","id":"150"}]

 

removeDuplicates: Remove duplicate objects using a key.

var employees = [
{'name' : 'John'},
{'name' : 'John'},
{'name' : 'Joe'},
{'name' : 'Sun'},
{'name' : 'Park'},
];
gs.info(JSON.stringify(ArrayObjectUtil.removeDuplicates(employees, 'name')));

Output: [{"name":"John"},{"name":"Joe"},{"name":"Sun"},{"name":"Park"}]

 

filterByKeys: Filter only objects that contains the given keys.

var employees = [
{'name' : 'John' , 'id': '111', 'email': 'john@gmail.com'},
{'name' : 'Park' , 'id' : '150', 'email': 'park@gmail.com'},
{'lastname' : 'bond' , 'firstname' : 'james', 'email': 'park@gmail.com'},
];

gs.info(JSON.stringify(ArrayObjectUtil.filterByKeys(employees, ['name','id'])));

Output: [{"name":"John","id":"111","email":"john@gmail.com"},{"name":"Park","id":"150","email":"park@gmail.com"}]

 

Edit: This script is added as part of resource management plugin, which is part of PPM.

 

I hope you found this blog interesting and useful.

NOTE: I was not able to find any reference to this script on community while searching. If someone has already talked about this, I would be happy to link the reference to this blog. 

2 Comments