Created
August 8, 2022 15:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
}, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.