JavaScript
x
2
1
var test_data = {'hr' : [1, 'Hour'], 'min' : [60, 'Minute'], 's' : [3600, 'Second']}
2
Now while I iterate, I want to get ‘s’ first-
JavaScript
1
4
1
$.each(test_data, function(i, j) {
2
// I need to get 's' first
3
});
4
I have tried test_data.revers()
which does not work. Any solution?
Advertisement
Answer
For a defined order, you could sort the keys of the object and iterate them in descending order of the first value of the array.
JavaScript
1
5
1
var data = { hr: [1, 'Hour'], min: [60, 'Minute'], s: [3600, 'Second'] };
2
3
for (const key of Object.keys(data).sort((a, b) => data[b][0] - data[a][0])) {
4
console.log(key, data[key]);
5
}