0

I am attempting to flatten an array and remove duplicates in one step using reduce and a Set but the array is flattened but the duplicates remain.

The related answer speaks to removing duplicates from an array but I would like to flatten and remove duplicates in one step, so I would suggest this might not be a duplicate question.

const wells = [
  [{
      id: 1,
      name: 'well one'
    },
    {
      id: 2,
      name: 'well two'
    },
    {
      id: 3,
      name: 'well three'
    },
  ],
  [{
      id: 2,
      name: 'well two'
    },
    {
      id: 3,
      name: 'well three'
    },
    {
      id: 4,
      name: 'well four'
    },
  ],
];


const uniqueflattenedWells = wells.reduce(
  (a, b) => Array.from(new Set(a.concat(b))), [],
);

console.log(uniqueflattenedWells)
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Bryan Dellinger
  • 4,724
  • 7
  • 33
  • 79

2 Answers2

2

You can use filter and every if IDs are ok to determine whether items are duplicated:

const wells = [[{"id":1,"name":"well one"},{"id":2,"name":"well two"},{"id":3,"name":"well three"}],[{"id":2,"name":"well two"},{"id":3,"name":"well three"},{"id":4,"name":"well four"}]];

const uniqueflattenedWells = wells.reduce(
  (a, b) => a.concat(b.filter(o => a.every(x => x.id !== o.id))),
  [],
);

console.log(uniqueflattenedWells)

Note: this method won't dedupe items coming from the same inner Array

blex
  • 24,941
  • 5
  • 39
  • 72
0

Objects are only equal when they are the exact same reference. One way to solve this problem is by using JSON.stringify to compare the objects, but note that this is not the fastest method.

const wells = [
  [{
      id: 1,
      name: 'well one'
    },
    {
      id: 2,
      name: 'well two'
    },
    {
      id: 3,
      name: 'well three'
    },
  ],
  [{
      id: 2,
      name: 'well two'
    },
    {
      id: 3,
      name: 'well three'
    },
    {
      id: 4,
      name: 'well four'
    },
  ],
];


const uniqueflattenedWells = ((set) => wells.reduce((a,b)=>a.concat(b), []).filter(obj => {
    const str = JSON.stringify(obj);
    return !set.has(str) && set.add(str);
  })
)(new Set);

console.log(uniqueflattenedWells)
Unmitigated
  • 76,500
  • 11
  • 62
  • 80