how do i get a unique array from an object via a new set?

You could take a hash table and get the values of it.

This approach takes the last found object with the same id.

const
    array = [{ id: 1, title: 'first' }, { id: 2, title: 'test2' }, { id: 1, title: 'last' }],
    unique = Object.values(array.reduce((r, o) => (r[o.id] = o, r), {}));

console.log(unique);

By using a Set with a closure, you could filter the array.

This approach takes the first found object with the same id.

const
    array = [{ id: 1, title: 'first' }, { id: 2, title: 'test2' }, { id: 1, title: 'last' }],
    unique = array.filter((s => ({ id }) => !s.has(id) && s.add(id))(new Set));

console.log(unique);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top