Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I am facing a rather unusual problem using _.object(underscore library call). Below is my code :

var sortable =  [ [ 'c', 107 ],[ 'd', 59 ],[ 'e', 53 ],[ '5', 53 ],[ '6', 26 ],[ '3', 19 ],[ 'a', 10 ],[ '8', 7 ],[ '2', 5 ], [ '7', 4 ],[ '1', 3 ], [ '9', 3 ]];
         sortDict = _.object(sortable);

My output should be :

sortDict = { 'c':107 ,'d': 59,'e': 53 ,'5': 53,'6': 26,'3': 19,'a':10,'8': 7 ,'2':5, '7': 4,'1': 3, '9': 3 }

But what I am getting has me confused, see the output below :

sortDict = {'1': 3, '2':5, '3': 19, '5': 53, '6': 26, '7': 4, '8': 7 , '9': 3 , 'c':107 , 'd': 59, 'e': 53 , 'a':10 }

I am just trying to convert an array to an object here, but the order seems to have changed. Can you please help me achieve my desired output by any means.

share|improve this question

marked as duplicate by undefined, cookie monster, Chris Haas, Andrew Whitaker Jun 11 at 15:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4  
There is no defined order to object keys in JavaScript objects, so your output is correct if the order is your only issue. –  Andrew Whitaker Jun 11 at 12:42
1  
1  
YOU CAN'T. If you maintain a separate Array and use that for the purpose of iteration, then do that. But then you might as well just use the original and not convert it. –  cookie monster Jun 11 at 12:48
1  
What spec? If it requires JSON, then it makes no sense that it requires the object to be sorted. –  cookie monster Jun 11 at 12:54
1  
If the spec doesn't make sense you could always ask for it to be corrected. And if you don't see the point of your own question here, you're welcome to delete it. –  source.rar Jun 11 at 13:06
show 15 more comments

2 Answers

up vote 1 down vote accepted
var sortable = [
    ['c', 107],
    ['d', 59],
    ['e', 53],
    ['5', 53],
    ['6', 26],
    ['3', 19],
    ['a', 10],
    ['8', 7],
    ['2', 5],
    ['7', 4],
    ['1', 3],
    ['9', 3]
];
var sorted = '';
sorted +='{';
for (var i = 0; i < sortable.length; i++) {
    sorted += "'"+sortable[i][0]+"': " +sortable[i][1]+",";
}
sorted +='}';
var obj = sorted;
alert(JSON.stringify(obj));
alert(sorted);
share|improve this answer
    
this creates that format, but cant be called as an object. –  user3365783 Jun 11 at 14:04
add comment

If you are required to send an actual JSON object in sorted order, you can do that, because you can always write the string version of it in a specific order (you have a sorted array, right?). (JSON fundamentally being a string representation of a Javascript object.) But the in-memory representation of that Javascript object has no order constraints and its elements will come back via for...in in the order determined by whatever Javascript engine processes it. Even Object.keys() doesn't guarantee an order, but it does give you something you can sort.

If the receiving end of your data also uses Javascript, specifying an ordered object is pointless because it won't iterate that way; if the receiving end is Java, C#, or C++, then its inability to handle the data in any supplied order is pretty much a weakness in the design of that code. There is no a priori reason an unordered object should be delivered in a specific order because that's the opposite of 'unordered' and indicates that the requirements were written by someone...well, not completely qualified. (In fact, the idea that the keys must be delivered in a certain order implies a certain dangerous dependency between leading and trailing keys, and that should make you uncomfortable with the design.)

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.