Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!