Finding the max value of an attribute in an array of objects

前端 未结 13 2332
不思量自难忘°
不思量自难忘° 2020-11-22 08:05

I\'m looking for a really quick, clean and efficient way to get the max \"y\" value in the following JSON slice:

[
  {
    \"x\": \"8/11/2009\",
    \"y\": 0         


        
13条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 08:26

    clean and simple ES6 (Babel)

    const maxValueOfY = Math.max(...arrayToSearchIn.map(o => o.y), 0);
    

    The second parameter should ensure a default value if arrayToSearchIn is empty.

提交回复
热议问题