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
Post a Comment