Skip to content

Instantly share code, notes, and snippets.

@wiedymi
Last active January 21, 2020 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wiedymi/08e931fc6d0e281a52432c6c3d371339 to your computer and use it in GitHub Desktop.
Save wiedymi/08e931fc6d0e281a52432c6c3d371339 to your computer and use it in GitHub Desktop.
function removeDublication(array, cb) {
if (!array) {
throw new Error("No array has been provided.");
}
if (!cb) {
throw new Error("No callback has been provided.");
}
return array.reduce(
(acc, value) => {
const isExist = acc.some(v => cb(v, value));
if (!isExist) {
acc = [...acc, value];
}
return acc;
},
[array[0]]
);
}
console.log(
removeDublication([{ id: 1 }, { id: 1 }, { id: 1 }], (v, val) => {
return v.id !== val;
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment