0

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?

StormTrooper
  • 1,731
  • 4
  • 23
  • 37
  • 1
    Use `allKeysFormatted` to access the values and don't try to mess with the order of the properties directly (this would only work with new objects and adding the properties in the "correct" order). – Andreas Oct 25 '21 at 12:13
  • 3
    Properties in objects in JS have no any specific order. – Teemu Oct 25 '21 at 12:13
  • 1
    You cannot guarantee the order of keys of object. They are purely random. there is no significace for the order of key of an object. – Nitheesh Oct 25 '21 at 12:14
  • 1
    @teemu actually [they do](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order#:~:text=YES%20(for%20non%2Dinteger%20keys,this%20and%20all%20browsers%20comply)). – pilchard Oct 25 '21 at 12:14
  • Make your life easier and don't do the stereotypical abuse of attempting to use object property order. Object keys were originally unordered (not anymore), but one should still treat them as such. Your code could be fixed, but only until it runs into some issue with e.g. one key being a number, and then you have to tear down all your written code, because it doesn't work anymore. – ASDFGerte Oct 25 '21 at 12:14
  • 1
    @pilchard They don't, the iteration methods are just creating a certain order. – Teemu Oct 25 '21 at 12:15
  • @Nitheesh they are not random, they follow a specific set of rules, but that doesn't mean they should be relied on. – pilchard Oct 25 '21 at 12:15
  • @teemu if iteration is the only way to access them doesn't that add up to them being ordered? – pilchard Oct 25 '21 at 12:16
  • @pilchard Yep, but depending the method the order might change. – Teemu Oct 25 '21 at 12:17
  • 1
    Agreed, but that doesn't mean they have no specific order, just that one needs to be aware of the rules by which their order may change. (I'm not advocating relying on object property order). – pilchard Oct 25 '21 at 12:17
  • Why not just create your own `each` method for the data? – Teemu Oct 25 '21 at 12:18
  • But actually I need to export this data in csv. so there the data must be in order. – StormTrooper Oct 25 '21 at 12:19
  • So access them via your array, don't expect every object to follow a set order – pilchard Oct 25 '21 at 12:19
  • Imho most informative question/answers for object property order is [does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties](https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties). Questions about object property order always descend into some chaos discussion, but still: if you need order, use another data structure, e.g. a `Map`, an array of property names in which to iterate, etc. And again: your code is fixable, doing so imho just won't be the proper solution on the long run. – ASDFGerte Oct 25 '21 at 12:23

0 Answers0