Skip to content

Instantly share code, notes, and snippets.

@aliakakis
Last active December 1, 2022 09:50
Show Gist options
  • Save aliakakis/3d1295db314029e0d6661b2fd6f13053 to your computer and use it in GitHub Desktop.
Save aliakakis/3d1295db314029e0d6661b2fd6f13053 to your computer and use it in GitHub Desktop.
/**
* Remove duplicate values from an array
* @param {[] | Object[]} obj - array of values or array of objects
* @param {string} [prop] prop - the property in the objects which will be used as a unique value
* @returns {[]}
*/
export const removeDuplicatesFromArray = (obj, prop) => {
return Object.values(obj.reduce((acc, cur) => {
return {
...acc,
[prop ? cur[prop] : cur]: cur
}
}, {}));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment