There is a dynamic array of objects with coordinates, you need to get an object with the maximum value of x:
var mass = [{ x: 34 }, { x: 12 }, { x: 2 }, { x: 348 }, { x: 15 }]; var maxObj = Math.max(mass[i].x); There is a dynamic array of objects with coordinates, you need to get an object with the maximum value of x:
var mass = [{ x: 34 }, { x: 12 }, { x: 2 }, { x: 348 }, { x: 15 }]; var maxObj = Math.max(mass[i].x); var maxObj=mass.reduce(function(prev,cur) { return cur.x>prev.x?cur:prev; },{x:-Infinity}); in ES6 it will be a bit shorter (and IMHO is not clearer):
let maxObj=mass.reduce((prev,cur) => cur.x>prev.x?cur:prev,{x:-Infinity}); Update
var is not going anywhere, and this is not coffee. let is different from var like this:
var i=1;for (var i=0;i<10;i++){};console.log(i); //10 var i=1;for (let i=0;i<10;i++){};console.log(i); //1 Source: https://ru.stackoverflow.com/questions/356219/
All Articles