티스토리 뷰

공부

[JS] Sort object property

승가비 2020. 2. 9. 02:53
728x90
var maxSpeed = {
    car: 300, 
    bike: 60, 
    motorbike: 200, 
    airplane: 1000,
    helicopter: 400, 
    rocket: 8 * 60 * 60
};
var sortable = [];
for (var vehicle in maxSpeed) {
    sortable.push([vehicle, maxSpeed[vehicle]]);
}

sortable.sort(function(a, b) {
    return a[1] - b[1];
});

//[["bike", 60], ["motorbike", 200], ["car", 300],
//["helicopter", 400], ["airplane", 1000], ["rocket", 28800]]

[reference] https://stackoverflow.com/questions/1069666/sorting-object-property-by-values

728x90

'공부' 카테고리의 다른 글

[Python] Selenium clear cache  (0) 2020.02.09
[Python] Selenium in private mode  (0) 2020.02.09
[JS] Sort array ASC, DESC  (0) 2020.02.09
[JS] unique array  (0) 2020.02.09
[Python] Subtract array  (0) 2020.02.09
댓글