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.

4
  • 2
    how does that matter? Commented Mar 27, 2016 at 8:25
  • Javascript can arrange object properties in alphabetical order. But in case of object, properties names order doesn't matter Commented Mar 27, 2016 at 8: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. Commented Mar 27, 2016 at 8: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. Commented Mar 27, 2016 at 8:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.