0

I have a list of objects:

[{name: 'Elza'}, {name: 'Tom'}, {name: 'Elza'}]

I use the below methods to get duplicated objects(by name) and assign a prop isDuplicated:

const duplicatedNames = arrayOfObjects
  .map(e => e['name'])
  .map((e, i, final) => final.indexOf(e) !== i && i++)
  .filter(obj => arrayOfObjects[obj])
  .map(e => !arrayOfObjects[e]['name']);
const result = arrayOfObjects.filter((obj, i) => {
  return duplicatedNames.includes(obj.name) && Object.assign(obj, { isDuplicated: true });
});

I receive an array like:

[{name: 'Elza', isDuplicated: true}, {name: 'Tom'}, {name: 'Elza', isDuplicated: true}]

I would like to mark only the second occurrence of duplicate- so i would like the result to be:

[{name: 'Elza'}, {name: 'Tom'}, {name: 'Elza', isDuplicated: true}]

Can anyone know how to do it base on my code?

4
  • Do you want to modify the original array, or return a new array with isDuplicated? Commented Jun 10, 2020 at 12:39
  • Better to return a new array Commented Jun 10, 2020 at 12:40
  • @anime say if 3 values with name Elja exists so you want to mark only last element as duplicate. is it so? Commented Jun 10, 2020 at 12:51
  • In my example only 2 max can be the same , hence my question was this way;) Commented Jun 10, 2020 at 13:01

5 Answers 5

3

Here is a function that checks if a name exist more then once.

let data = [{name:'Elza'}, {name:'Tom'}, {name:'Elza'}, {name: "Jerry"}, {name: "Jerry"}];

function checkDup(arr){
   let cache = [];
   return arr.map(({name}, index) => {
      if(!cache.find(el => el.name == name)){
         cache.push({name, index});
         return {name, index};
      }
      let { index: cacheIndex } = cache.find(el => el.name === name);
      return {name,index: cacheIndex , isDuplicated: true};
   })
}

console.log(checkDup(data));

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

4 Comments

And what if I would like to add to each object index prop and 'reset' these which are duplicated. Is it doable? So i would like to have something like this: [{name:'Elza', index:0}, {name:'Tom', index:1}, {name:'Elza', index:0, duplicated:true}, {name:'Jerry', index:2}]
@anime check it out, i think that is what you want
Thank you so much- yeah, it was exactly what i wanted. Thank you sooooooo much!!!!
@anime i found a mistake, please take a look at the upated code
3

You could create a Set of names. If the size of the set is same as after the name has been added, then it's duplicate record.

const input = [{name:'Elza'}, {name:'Tom'}, {name:'Elza'}],
      names = new Set;

for (const o of input)
  if (names.size === names.add(o.name).size)
    o.isDuplicate = true

console.log(input)

Comments

0

You can try this:

let users = [{name:'Elza'}, {name:'Tom'}, {name:'Elza'}]

let flags = [], output = [];
users.forEach(user => {
  if (flags[user.name]) {
    output.forEach(item => {
      if (item.name === user.name) {
        item.isDuplicated = true
        output.push(user);
      }
    })
  } else {
    flags[user.name] = true;
    output.push(user);
  }
})

Comments

0

Given your original array A, you could create a temporary array B and, for each a element of A, check:

  • if B contains a.name, then set a.isDuplicated to true;
  • else, push a.name in B.

let A = [{name: 'Elza'}, {name: 'Tom'}, {name: 'Elza'}];
let B = [];
A.forEach(a => {
  if (B.includes(a.name)) {
    a.isDuplicated = true;
  } else {
    B.push(a.name);
  }
});
console.log(A);

Comments

0

You can use reduce with a helper object:

const collection = [{ name: 'Elza'}, { name: 'Tom'}, { name: 'Elza' }]

const helper = {}

const result = collection.reduce((acc, { name }) => {
  if (helper[name]) {
    return [...acc, { name, isDuplicate: true }]
  }
  helper[name] = 'x';
  return [...acc, { name }]
}, [])

console.log(result)

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.