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)

2
  • Objects aren't === to each other, so a Set won't de-duplicate Commented Dec 9, 2020 at 17:53
  • I edited the question to have it re opened as the suggested answer talks of removing duplicates but I would like to flatten and remove duplicates in one step. so hopefully it will be reopened Commented Dec 9, 2020 at 17:59

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.