logo

NJP

Unlearn Series - ArrayObjectUtil - The long-lost cousin

Import · May 12, 2022 · article

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': '[email protected]'},
];

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

Output: [email protected]

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' : '[email protected]' , 'id': '111'};
gs.info(JSON.stringify(ArrayObjectUtil.combineObjects(employeeSet1,employeeSet2)));

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

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': '[email protected]'},
{'name' : 'Park' , 'id' : '150', 'email': '[email protected]'},
{'lastname' : 'bond' , 'firstname' : 'james', 'email': '[email protected]'},
];

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

Output: [{"name":"John","id":"111","email":"[email protected]"},{"name":"Park","id":"150","email":"[email protected]"}]

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.

View original source

https://www.servicenow.com/community/developer-blog/unlearn-series-arrayobjectutil-the-long-lost-cousin/ba-p/2265907