Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
How can add properties to my objects based on the duplicates inside the array?

I have this array of objects

let arr = [
    {
        id: 1,
    },
    {
        id: 1,
    },
    {
        id: 2,
    },
    {
        id: 1,
    },
    {
        id:4,
    },
    {
        id: 3,
    },
    {
        id:4,
    }
]

i need to find and change every object in the array based on condition. So if there are duplicates in the array i need to set on my objects 100 except last duplicate where i should have 200. If i don't have any duplicates than i should have again 200

So the output shpuld be

let arr = [
    {
        id: 1,
        number: 100
    },
    {
        id: 1,
        number: 100
    },
    {
        id: 2,
        number: 200
    },
    {
        id: 1,
        number: 200
    },
    {
        id:4,
        number: 100
    },
    {
        id: 3,
        number: 200
    },
    {
        id:4,
        number: 200
    }
]

so id 1 has duplicates. That is why the fiurst occurences are set with number:100 and the last one i set with number:200.

Id 2 has number 200 because there are no duplicates and it is first occurance in the list.

what i tried

I got stuck at

for(let item of arr) {
    for(let item2 of arr) {
        if(item.id === item2.id) {
            item.number = 100;
        } else {
            item.number = 200;
        }
    }
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can simply iterate through the array in reverse and track which ids you've seen, here using a Set.

const arr = [{ id: 1, }, { id: 1, }, { id: 2, }, { id: 1, }, { id: 4, }, { id: 3, }, { id: 4, }]

let i = arr.length;
const seen = new Set();

while (i--) {
  arr[i].number = seen.has(arr[i].id) ? 100 : 200;
  seen.add(arr[i].id)
}

console.log(arr)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use array.map() to iterate over your array. I think it can provide a nice and concise solution:

const result = arr.map((item, index) => {
  const duplicate = arr.filter((_, indx) => indx > index).some((i) => i.id === item.id);
  return { ...item, number: duplicate ? 100 : 200 }
});

console.log(result);
about 4 years ago · Juan Pablo Isaza Denunciar

0

We can simply achieve it via Array.map() along with Array.indexOf() & Array.lastIndexOf() methods.

Working Demo :

// Input array
let arr = [{
  id: 1,
}, {
  id: 1,
}, {
  id: 2,
}, {
  id: 1,
}, {
  id:4,
}, {
  id: 3,
}, {
  id:4,
}];

// Getting ID's from each object and create a seperate array
let idArr = arr.map(function(item) { return item.id });

// Iterating through the id's array and assigning number property to an original array as per the requirement.
idArr.forEach((item, index) => {
  if (idArr.indexOf(item) === idArr.lastIndexOf(item)) {
    arr[index].number = 200;
  } else {
    arr[index].number = 100;
    arr[idArr.lastIndexOf(item)].number = 200;
  }  
});

// Result
console.log(arr);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda