1

I have searched StackOverflow and found this answer, but it does not solve my problem.

My problem is, my array is like this:

let arr = [
  {type: 1, id: 1, name:'aa'},
  {type: 1, id: 1, name:'bb'},
  {type: 2, id: 1, name:'cc'}
]

And I need to find same type and same id and then recognize that this is duplicate object. Above, arr[0] and arr[1] is a duplicate but arr[0] and arr[2] are not. I have tried using ES6 methods such as .some(), .every(), and a Set(), but non of these have worked for me. How can I solve this problem?

2
  • 1
    what is your expected result? A boolean? The array with the duplicates removed? Commented Feb 25, 2020 at 8:04
  • yes,I just need Boolean result. Commented Feb 25, 2020 at 8:07

3 Answers 3

3

You can use .map() to map each object to a string. Each string takes the shape of type-id for that object. Then, using a Set you can remove all duplicate strings. You can then check if the set size equals the array length to determine if there are any duplicates or not in the array:

const containsDups = arr => new Set(arr.map(({type, id}) => `${type}-${id}`)).size !== arr.length;

const arr = [{type: 1, id: 1, name:'aa'},{type: 1, id: 1, name:'bb'},{type: 2, id: 1, name:'cc'}];
console.log(containsDups(arr));

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

2 Comments

Why Set() can not determine Object duplicates?I have try let set = new Set();set.add({type:1,id:1});set.add({type:1,id:1});console.log(set.size) // 2(why?)
@yang because objects are reference types, so two objects are not the same if they’re different reference types. So {} != {} will return true. Since object equality can not be determined like this, the set also cannot determine when two objects as the same. So, you need to use a string, where equality where work
0

It is possible to group by properties such as type and id and then check whether names more than 1. If yes, it means, that there is at least one duplicate:

const result = [...arr.reduce((r, o) => {
    const key = o.type + '-' + o.id;
    const item = r.get(key) || Object.assign({}, o, {
        names: []
    });
    item.names.push(o.name);
    return r.set(key, item);
}, new Map).values()];

An example:

let arr = [{type: 1, id: 1, name:'aa'},{type: 1, id: 1, name:'bb'},{type: 2, id: 1, name:'cc'}]

const result = [...arr.reduce((r, o) => {
    const key = o.type + '-' + o.id;
    const item = r.get(key) || Object.assign({}, o, {
        names: []
    });
    item.names.push(o.name);
    return r.set(key, item);
}, new Map).values()];

console.log(`Is there duplicates: `, result.some(s => s.names.length > 1));

Comments

0

you can use Set:

Set is a new data object introduced in ES6. Because Set only lets you store unique values

const seen = new Set();

const arr = [
  {type: 1, id: 1, name:'aa'},
  {type: 1, id: 1, name:'bb'},
  {type: 2, id: 1, name:'cc'}
];

const filteredArr = arr.filter((el, index) => {
  const duplicate = seen.has(el.type, el.id);
  seen.add(el.type, el.id);
  console.log('index: '+ index +' is duplicate: '+ duplicate);
  return !duplicate;    //  if you just need a boolean, this is what you are looking for.
});

console.log('new array: ' + JSON.stringify(filteredArr));

1 Comment

The .add and .has method only accepts one value. So if the second object has the same type but different id to the first it will still be removed

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.