Notice
Recent Posts
Recent Comments
«   2024/10   »
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 30 31
Archives
관리 메뉴

함번보고 두번보고

[Javascript] Map객체의 Value 최댓값 구하기.(by reduce()) 본문

Front-End/Javascript

[Javascript] Map객체의 Value 최댓값 구하기.(by reduce())

Hamstar_ 2021. 2. 28. 13:37

reduce 함수를 사용해 Map의 value 최댓값을 구할 수 있다.

let arr = [
    ['bob', 1], 
    ['a', 1],
    ['ball', 2],
    ['the', 1],
    ['flew', 1],
    ['far', 1],
    ['after', 1],
    ['it', 1],
    ['was', 1]
]

let map = new Map(arr);

console.log([...map.entries()].reduce((a,b) => a[1] > b[1] ? a : b)[0]);

// ball

 

 

Comments