-3

From the array test the two objects with "id": 3 should be evaluated as a duplicte

{ "test": [
           {"id": 8, "num": 11 },
           {"id": 3, "num": 10 },
           {"id": 3, "num": 12 },
  ]
}

How to find objects in a json array where a certain attribute (here id) has the same value (here 3)?

5
  • Apart from the id, I see no repeated value, num is unique in each field, could you be more specific ? Please show us what you have tried too :) Commented May 3, 2019 at 11:49
  • @jo_va here the same id should not repeat if so it should return an error Commented May 3, 2019 at 11:51
  • Use a simple for loop and add each id to an array or a Set. Check if the current id in context exists in the array. Have you tried anything? Please read How much research effort is expected of Stack Overflow users? Commented May 3, 2019 at 11:58
  • 2
    please add the code, you tried and the wanted result. Commented May 3, 2019 at 11:59
  • 2
    Possible duplicate of How can I check if the array of objects have duplicate property values? Commented May 3, 2019 at 12:00

1 Answer 1

8

We can do so by making use of Array.some and Set.

The short version is this

const hasDuplicate = obj.test.some(element =>idSet.size === idSet.add(element.id).size)

A simplyfied example

const myArray = [{"id": 5},{"id": 6},{"id": 7},{"id": 7},{"id": 8}];
var idSet = new Set();
const hasDuplicate = myArray.some(element =>idSet.size === idSet.add(element.id).size);
// elements are added until the first duplicate was found; 
// that is the reason why id=8 is not in the set
let allEntries = "";
for (const entry of idSet.entries()) {
  allEntries += entry + ", "   
}
console.log(allEntries);

Your example with console.log to illustrate what goes on

const obj = { "test": [
           { "id": 7, "num": 112},
           { "id": 8, "num": 11},
           { "id": 3, "num": 10 },
           { "id": 3, "num": 12 },
           { "id": 9, "num": 12 }
        ]};
const idSet = new Set(); // a Set stores unique values of any type
// for each element in test[] try to add the id to idSet 
const hasDuplicate = obj.test.some(element => {                       
  console.log("id ", element.id, " is already in set: ", idSet.has(element.id));
  // returns true for the first duplicate and terminates .some()
  return (idSet.size === idSet.add(element.id).size)
});

console.log("hasDuplicate =", hasDuplicate); // returns true for 2nd id = 3 

What we are trying to achieve here is

  • we try to add each object from the obj.test array into the Set idSet
  • every entry within the Set element must be unique.

If and when duplicated ids are found, it cannot be added to the Set. Therefore, the size of the Set, unique, will not increase for that iteration, and thus, it will cause Array.some() to return true, which denotes that there is indeed a duplicated id.

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

3 Comments

in above addition i want index also for duplicated vale of latest item index
works like charm!

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.