How can I iterate over a javascript object, from back to front.
The object looks like this. {"33":140, "34":100, "35":120, "36":200}
I want it to display like this...
36 | 200
35 | 120
34 | 100
33 | 140
I tried sorting first then displaying, but it sorts by the second number, not the key. How would I either iterate from back to front, or reverse sort based on the key.
I realize this is pretty simple, but Im getting pretty frustrated with it....
A fairly modern version would look like this:
Object.keys(obj).sort(function (a, b) {
return Number(b) - Number(a);
}).forEach(function (current) {
console.log(current + ' | ' + obj[current]);
});
Similarly, but with a little bit more code, it could be written for older browsers, too. Or you use shims.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.