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

124
Vistas
JavaScript: check if duplicate key values exist in array of objects, if there are, rename it

I have an array "source"

source : [
{
    "id": 1,
    "secondId": 1
},
{
    "id": 2,
    "secondId": 1
},
{
    "id": 3,
    "secondId": 1
}
]

I want to rename the secondId when there are duplicate like this:

[
{
    "id": 1,
    "secondId": 1
},
{
    "id": 2,
    "secondId": 1_2
},
{
    "id": 3,
    "secondId": 1_3
}
]

I have this so far:

for (i = 0 ; i < source.length ; i++) {
  for (j = 0 ; j < source.length ; j++){

    if (source[i]["id"] != source[j]["id"] && source[i]["secondId"] === source[j]["secondId"]){
        source[j]["secondId"] += "_" + (i+1);
    }
    console.log(source[j]["secondId"]);

  }
}

and I'm getting:

[
{
    "id": 1,
    "secondId": 1
},
{
    "id": 2,
    "secondId": 1_2
},
{
    "id": 3,
    "secondId": 1_2_3
}
]

I tried to use some:

if(source[j]["secondId"].includes("_"+ (i+1))){
    console.log(source[j]["secondId"].split("_"+ (i+1)).shift());
}

but I'm getting:

"secondId": 1 "secondId": 1 "secondId": 1_2

How can I do it? Any tips please?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

A version using Array.reduce:

let source = [
{
    "id": 1,
    "secondId": 1
},
{
    "id": 2,
    "secondId": 1
},
{
    "id": 3,
    "secondId": 1
}];

let output = Object.values(
  source.reduce((a, e, i) => {
    let testId = e.secondId.toString();
    if (a[testId]) {
      testId = testId.split("_")[0] + "_" + (i + 1);
    }
    a[testId] = {...e, secondId: testId};
    return a;
  }, {})
);

console.log(output);

about 4 years ago · Juan Pablo Isaza Denunciar

0

This may be a solution to achieve the (assumed) desired objective:

Code Snippet

const markDupes = arr => (
  arr.reduce(
    (fin, {id, secondId}) => ({
      tm: {
        [secondId]: (fin.tm[secondId] || 0) + 1
      },
      newArr: [
        ...fin.newArr,
        {id, secondId: secondId.toString() + (
          fin.tm[secondId] > 0 ? `_${fin.tm[secondId]+1}` : ''
        )}
      ]
    }),
    { tm: {}, newArr: [] }
  )?.newArr
);


const source = [{
    "id": 1,
    "secondId": 1
  },
  {
    "id": 2,
    "secondId": 1
  },
  {
    "id": 3,
    "secondId": 1
  }
];


console.log(markDupes(source));

Explanation

  • Use .reduce to iterate over the array of objects
  • Set up an initial fin object with two props tm (tracking-map) and newArr (the result-array which will have the secondId updated as desired)
  • For each object, destructure to directly access id and secondId
  • Update the map tm based on the secondId with a counter
  • Append to the newArr an object with id and secondId props with the latter (secondId) being converted to a string to store values of the format 1_2, 1_3, etc

NOTE: This may not be an optimal solution.

about 4 years ago · Juan Pablo Isaza Denunciar

0

When you convert 1 to 1_2 or 1_3 it is converting a number to a string which will be a huge pain when you have a use for the number later. Instead what i have done is convert that number to a decimal for as 1.2 ,1.3 which means you can do all sorts of computation on a number without much conversion

let source = [
    {
        "id": 1,
        "secondId": 1
    },
    {
        "id": 2,
        "secondId": 1
    },
    {
        "id": 3,
        "secondId": 1
    }
];

let val = {};
for (const i in source) {
    let v = source[i].secondId
    val[v] = val[v] ? val[v] : v
    if (val[v] !== 1) {
        console.log(source[i].id, v);
        source[i].secondId = Number(v.toFixed(2)) + (val[v] * 0.1)
    }
    val[v]++
}
console.log(source);

if you are really kean of using string instead use source[i].secondId = v+'_'+val[v] instead inside the if by changing the original source json object as well

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