find maximum number in array of objects?

how I can find the bigger and smaller number in the most efficient way in javascript (or the most easiest way)

something like this:

function findMaxFromArray() {
  // logic
  return { x: ..., y: ... }
}

I tried Math.max(array.x) and is not working

here the array:

let array = [{
    "x": 0,
    "y": 0
  },
  {
    "x": 3.6417243968365076,
    "y": 10
  },
  {
    "x": 9.34437089784549,
    "y": 20
  },
  {
    "x": 0.7464057532722523,
    "y": 30
  },
  {
    "x": 10.254345841653576,
    "y": 40
  },
  {
    "x": 40.665266471734526,
    "y": 50
  },
  {
    "x": 13.605184580988041,
    "y": 60
  },
  {
    "x": 56.1970151503722,
    "y": 70
  },
  {
    "x": 46.30684270171864,
    "y": 80
  },
  {
    "x": 86.97417514998948,
    "y": 90
  },
  {
    "x": 19.086639005143002,
    "y": 100
  },
  {
    "x": 74.53073691009028,
    "y": 110
  },
  {
    "x": 80.51273650018987,
    "y": 120
  },
  {
    "x": 125.67651066602721,
    "y": 130
  },
  {
    "x": 94.92603682139705,
    "y": 140
  },
  {
    "x": 81.41258620429245,
    "y": 150
  },
  {
    "x": 128.79236053348222,
    "y": 160
  },
  {
    "x": 101.73294154449238,
    "y": 170
  },
  {
    "x": 176.92343490767377,
    "y": 180
  },
  {
    "x": 148.01122343809243,
    "y": 190
  },
  {
    "x": 13.666705559256798,
    "y": 200
  },
  {
    "x": 190.95447678085023,
    "y": 210
  },
  {
    "x": 186.02329838354444,
    "y": 220
  },
  {
    "x": 207.04643642736238,
    "y": 230
  },
  {
    "x": 75.3481702154557,
    "y": 240
  },
  {
    "x": 49.14228670472814,
    "y": 250
  },
  {
    "x": 203.65260758421869,
    "y": 260
  },
  {
    "x": 69.53501551989147,
    "y": 270
  },
  {
    "x": 269.52487694376975,
    "y": 280
  },
  {
    "x": 184.31351948105274,
    "y": 290
  },
  {
    "x": 19.72787138830958,
    "y": 300
  }
]

result output:

basically, we give an array of objects with x, y as a parameter to a function.

and the function should return the maximum x and maximum y

(for now, I don’t need to calculate the minimum since is always 0, but it will help as well as a solution in that topic too)

// output sample
return { 
  x: 269.52487694376975, 
  y: 300 
}

// or like this if it return also minimum

return {
  max: {
    x: 269.52487694376975,
    y: 300
  },
  min: {
    x: 0
    y: 0
  }
}

I have tried before, but with for loops, and isn’t giving me any result at all. and a lot of code. (if possible a simple way)

thanks

>Solution :

Use Array.prototype.map and Math.max

let array = [{
    "x": 0,
    "y": 0
  },
  {
    "x": 3.6417243968365076,
    "y": 10
  },
  {
    "x": 9.34437089784549,
    "y": 20
  },
  {
    "x": 0.7464057532722523,
    "y": 30
  },
  {
    "x": 10.254345841653576,
    "y": 40
  },
  {
    "x": 40.665266471734526,
    "y": 50
  },
  {
    "x": 13.605184580988041,
    "y": 60
  },
  {
    "x": 56.1970151503722,
    "y": 70
  },
  {
    "x": 46.30684270171864,
    "y": 80
  },
  {
    "x": 86.97417514998948,
    "y": 90
  },
  {
    "x": 19.086639005143002,
    "y": 100
  },
  {
    "x": 74.53073691009028,
    "y": 110
  },
  {
    "x": 80.51273650018987,
    "y": 120
  },
  {
    "x": 125.67651066602721,
    "y": 130
  },
  {
    "x": 94.92603682139705,
    "y": 140
  },
  {
    "x": 81.41258620429245,
    "y": 150
  },
  {
    "x": 128.79236053348222,
    "y": 160
  },
  {
    "x": 101.73294154449238,
    "y": 170
  },
  {
    "x": 176.92343490767377,
    "y": 180
  },
  {
    "x": 148.01122343809243,
    "y": 190
  },
  {
    "x": 13.666705559256798,
    "y": 200
  },
  {
    "x": 190.95447678085023,
    "y": 210
  },
  {
    "x": 186.02329838354444,
    "y": 220
  },
  {
    "x": 207.04643642736238,
    "y": 230
  },
  {
    "x": 75.3481702154557,
    "y": 240
  },
  {
    "x": 49.14228670472814,
    "y": 250
  },
  {
    "x": 203.65260758421869,
    "y": 260
  },
  {
    "x": 69.53501551989147,
    "y": 270
  },
  {
    "x": 269.52487694376975,
    "y": 280
  },
  {
    "x": 184.31351948105274,
    "y": 290
  },
  {
    "x": 19.72787138830958,
    "y": 300
  }
]

const max = {
  x: Math.max(...array.map(e => e.x)),
  y: Math.max(...array.map(e => e.y)),
}

console.log(max)

Leave a Reply