Kyounghwan01 / blog-comment

blog 코멘트 링크 레포
0 stars 0 forks source link

blog/JS/JSbasic/object-sort/ #47

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

JavaScript - 객체 value로 정렬하기 | 기억보다 기록을

JavaScript - 객체 value로 정렬하기, object value sorting, js, JS, object, array, sort

https://kyounghwan01.github.io/blog/JS/JSbasic/object-sort/

securekim commented 1 year ago

첫번째 예제에서 vehicle 이 아니라 name이 되어야 합니다.

sortable.push([vehicle, champ[name]]); 변경 후 -> sortable.push([name, champ[name]]);

예제 수정본


const champ = {
  Aatrox: 300,
  Zoe: 60,
  Ahri: 200,
  Ashe: 1000,
  Garen: 400
};
var sortable = [];
for (var name in champ) {
  sortable.push([name, champ[name]]);
}

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

// [["Zoe", 60], ["Ahri", 200], ["Aatrox", 300] ...```
Kyounghwan01 commented 1 year ago

@securekim 감사합니다!