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

113
Vistas
Increment Object set javascript

I have an object set

var obj = {
 ref01:{
 test:"abc"
},
ref02:{
 test:"xyz"
},
ref03:{
 test:"pqr"
}
}

and I have new object

const test = {
  test:"def"
}

I want to update the object above with increment value of ref Now ref is03 , I want to append like ref04 and increment each time based on existing obj.

var obj = {
 ref01:{
 test:"abc"
},
ref02:{
 test:"xyz"
},
ref03:{
 test:"pqr"
},
ref04:{
 test:"def"
}
}

i tried object.assign but its directly passing the value . obj = object.assign(obj,{ref:test})

So how can i get it ?

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

0

I think you want a list: i.e.

const obj = {
    testObjects: [
        {
            test: 'abc'
        },
        ...
    ]
}

then you can just run:

obj.testObjects.push({test: 'xyz'});

OR if this is an API response you can't change the schema of:

//find last index so you can add the ref obj
const getFormattedIndex = (idx) => String(idx).padStart(2, '0');
let i = 1;
while(obj[`ref${getFormattedIndex(i)}`]) {
   i++;
}
//add object with the correct sequential key
obj[`ref${getFormattedIndex(i)}`] = {test: 'xyz'};

or you could be a bad person and use scope-bleed with a ghetto for loop 😇:

//find last index so you can add the ref obj
const getFormattedIndex = (idx) => String(idx).padStart(2, '0');
for (var i = 1; obj[`ref${getFormattedIndex(i)}`]; i++);
//add object with the correct sequential key
obj[`ref${getFormattedIndex(i)}`] = {test: 'xyz'};
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