0

I have an array of object which might include duplicate values.

I want those array index variable value to true if those indexes has duplicate values.

Let say I have below array of objects

let arr = [
    {
        name :'abc',
        age : 20,
    },

    {
        name :'xyz',
        age : 25,
    },

    {
        name :'pqr',
        age : 22,
    },

    {
        name :'abc',
        age : 27,
    },

    {
        name :'abc',
        age : 26,
    },
]

so there is 3rd and 4th index which has duplicate name as 0th index. I want to set isError variable to true for 3rd and 4th index and for others I would like to set to false.

Any help would be great.

Thank you.

3 Answers 3

1

Use a Set to store existing names, and map the array. Check in the Set, if a name exists, and assign to the isError variable. Add the current name to the Set. Create a new object with the original content, and isError:

const markDuplicates = arr => {
  const existingIds = new Set();
  
  return arr.map(o => {
    const isError = existingIds.has(o.name)
    
    existingIds.add(o.name)
    
    return { ...o, isError };
  })
}

const arr = [{"name":"abc","age":20},{"name":"xyz","age":25},{"name":"pqr","age":22},{"name":"abc","age":27},{"name":"abc","age":26}]

const result = markDuplicates(arr)

console.log(result)

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

Comments

0

You can first sort the array & then use map. After sorting all the objects will be sorted in ascending order of name. Then use map which will return a new array and while returning check if the name in current object is same with previous object. If so then add a duplicate key and assign a value to it

 if (index !== 0 && item.name == k[index - 1].name) {

This line is omitting the check for first object in sorted array, because there is nothing to copy with previous object and item.name == k[index - 1].name is checking if the name is same with previous object

let arr = [{
    name: 'abc',
    age: 20,
  },

  {
    name: 'xyz',
    age: 25,
  },

  {
    name: 'pqr',
    age: 22,
  },

  {
    name: 'abc',
    age: 27,
  },

  {
    name: 'abc',
    age: 26,
  },
];
let k = arr.sort(function(a, b) {
  return a.name.localeCompare(b.name);
});
let z = k.map(function(item, index) {
  if (index !== 0 && item.name == k[index - 1].name) {
    return {
      name: item.name,
      age: item.age,
      duplicate: true
    }
  } else {
    return {
      name: item.name,
      age: item.age
    }
  }
});
console.log(z)

Comments

0

You can use a object as tracker, add a key by name,

Loop through data if the name is already available on op as key then change current element isError property to true else create a new key on op and set current element isError to false

let arr = [{name :'abc',age : 20,},{name :'xyz',age : 25,},{name :'pqr',age : 22,},{name :'abc',age : 27,},{name :'abc',age : 26,},]

let op = {}

arr.forEach( (inp,index) => {
  if( op[inp.name] ){
    inp.isError = true
  } else{
    inp.isError = false
    op[inp.name] = inp
  }
})

console.log(arr)

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.