0

In my Javascript application, I have an array with few properties var sampleArray = ["Number_Info", "Additional_Ids","Additional_Ids_2","_summary"];Now I need to convert this array into an object containing properties in the same order, as shown below :

var sampleObject = {
    "Number_Info" : {}, 
    "Additional_Ids" : {},
    "Additional_Ids_2" : {},
    "_summary" : {}
}

I tried the following :

var orderingObj = {};
for(var i=0; i < sampleArray.length; i++ ){
orderingObj[sampleArray[i]] = {}
}

But here I am getting object properties in different order.

 var orderingObj = {
"Additional_Ids_2" : {},
"Additional_Ids" : {},
"Number_Info" : {},
 "_summary" : {}
};

How can I fix it? From earlier posts the solutions were there to sort by names but here I have a specific order in which I want to order my properties.

Madasu K
  • 1,583
  • 2
  • 29
  • 62
  • 2
    how does that matter? – Cyril Cherian Mar 27 '16 at 08:25
  • Javascript can arrange object properties in alphabetical order. But in case of object, properties names order doesn't matter – RomanPerekhrest Mar 27 '16 at 08:28
  • I populate each of these properties values (in my question I have put empty objects) and display them on the front end, using for loop I am iterating each of these properties and display them. I want these properties displayed on the front end should be as shown in the array properties order. Please let me know, if I need to give any further clarification on this. – Madasu K Mar 27 '16 at 08:32
  • `for(var key in orderingObj) console.log(key);` - I think you're getting confused by the behaviour of the developer console (which orders the displayed object alphabetically), and the enumeration order if you were to actually iterate over the object. – Emissary Mar 27 '16 at 08:47

0 Answers0