Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda