Skip to content

Instantly share code, notes, and snippets.

@bhaireshm
Created August 8, 2022 15:11
Show Gist options
  • Save bhaireshm/59ecaec62c3b113dbbb0f695abbbe46e to your computer and use it in GitHub Desktop.
Save bhaireshm/59ecaec62c3b113dbbb0f695abbbe46e to your computer and use it in GitHub Desktop.
Compares all the objects(both key and value) in the given array and returns the unique array of objects.
/**
* Compares all the objects(both key and value) in the given array and returns the unique array.
*
* @param {Object[]} arr - An array of objects.
* @returns unique array of object(s).
*
* @example uniqueArrayOfObjects([{a: 2}, {a: 2}]); // [{"a": 2}]
* @example uniqueArrayOfObjects([{a: {b: 2}}, {a: {b: 2}}]); // [{"a": {"b": 2}}]
* @example uniqueArrayOfObjects([{a: 2}, {a: 2, b: 3}]); // [{a: 2}, {a: 2, b: 3}]
*/
function uniqueArrayOfObjects(arr) {
return arr.reduce((acc, curr) => {
if (acc.length > 0) {
acc.forEach((a) => {
/** Refer compareObject method in https://gist.github.com/bhaireshm/23765d970901e5da9507162796a358de */
if (a && !compareObject(a, curr)) acc.push(curr);
});
} else acc.push(curr);
return acc;
}, []);
}
@bhaireshm
Copy link
Author

bhaireshm commented Feb 24, 2025

ez.js is more than just a library; it's a coding companion that simplifies complex tasks. Whether you're dealing with arrays, numbers, objects, or strings, ez.js has got your back! Say goodbye to coding hassles and hello to streamlined JavaScript magic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment