Warm tip: This article is reproduced from stackoverflow.com, please click
ecmascript-6 javascript

How to remove duplicates by key-value in javascript object list?

发布于 2020-03-31 22:59:25

Consider the following array:

[
  { url: "https://url.com/file1", md5: "fbbbabcc19264ce7b376ce4c726b9b85" },
  { url: "https://url.com/file2", md5: "d920d140432b961f07695ec34bd2a8ad" },
  { url: "https://url.com/file3", md5: "fbbbabcc19264ce7b376ce4c726b9b85" },
  { url: "https://url.com/file4", md5: "bf80655dbe90123324f88a778efa39f7" },
  { url: "https://url.com/file5", md5: "fbbbabcc19264ce7b376ce4c726b9b85" }
];

The files "file1", "file3", and "file5" have the same content and therefore the same md5. I only want to keep the files with different md5s (file1, file2, file4).

What are the possible ways to achieve this with modern ES6?

Questioner
Latz
Viewed
54
Cygra 2020-01-31 19:51

You can use reduce to solve the problem:

const list = [
    { url: 'https://url.com/file1', md5: 'fbbbabcc19264ce7b376ce4c726b9b85' },
    { url: 'https://url.com/file2', md5: 'd920d140432b961f07695ec34bd2a8ad' },
    { url: 'https://url.com/file3', md5: 'fbbbabcc19264ce7b376ce4c726b9b85' },
    { url: 'https://url.com/file4', md5: 'bf80655dbe90123324f88a778efa39f7' },
    { url: 'https://url.com/file5', md5: 'fbbbabcc19264ce7b376ce4c726b9b85' },
];

const removeDup = (arr, key) => {
    return arr.reduce((acc, cur) => {
        if (acc.some(a => a[key] === cur[key])) return acc;
        return acc.concat(cur)
    }, [])
};

console.log(removeDup(list, 'md5'))