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

112
Vistas
Object key value position disorderd in javascript

I have a very simple use case scenario:

I have 2 arrays:

var mainArray = [{"name": "Mike", "gender": "male"}, {"name": "Tom", "gender": "male", "email": "tom@yahoo.com"}, {"name": "John", "gender": "male", "phoneNumber": "123456"}];
                
var allKeysFormatted = ["name", "gender", "email", "phoneNumber"];

mainArray.forEach(function(arrObj){
    allKeysFormatted.forEach(function(key){
        arrObj[key] = arrObj[key] || null;
    });
});
            

console.log(JSON.stringify(mainArray));

I want to have all the indexes in the mainArray in the same order as allKeysFormatted .If the key is not in the object, it will place there with null value.

But the problem I am facing is the position of keys are not maintaining its position, it gets changed. Like sometime I get email in the very first place of object.

Can someone help me with this?

about 4 years ago · Juan Pablo Isaza
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