arrays - ordering keys from Javascript object -


i saw this topic , answers, doesn't seem work me. tried alll variants of values in object , sort, doesn't seem work. want list ordered on keys (not values) in objects (the, in example 2 objects in in json):

d3.json:

{ "test1":    {"purchase": ["yes", 2], "safety": ["no", 3], "quality": ["carried husk", 1], "name": ["eggs!", 0]}, "test2":    {"purchase": "yes", "safety": "no", "quality": "carried husk", "name": "0"} 

}

and javascript:

d3.json("d3.json", function(root) {    (key in root) {       console.log(root[key]);       var test = root[key];       var list = object.keys(test).sort(function(a,b){ return test[a]-test[b] })       console.log(list);   } }); 

edit: apologies, wasn't clear on expected results: looking keys sorted, return values answer of dlopez did.

try this:

var sortedbykeys = object.keys(root)     .sort(function(a,b) { return - b; })     .reduce(function(prev, curr) {         prev[curr] = root[curr];         return prev;     }, {}); 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -