티스토리 뷰

공부

[JS] Sort object property

seunggabi 승가비 2020. 2. 9. 02:53
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

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

[Python] Selenium clear cache  (0) 2020.02.09
[Python] Selenium in private mode  (0) 2020.02.09
[JS] Sort object property  (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
댓글
댓글쓰기 폼
공지사항
최근에 달린 댓글
Total
28,468
Today
5
Yesterday
49
«   2020/02   »
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
글 보관함