Get the max value from object inside javascript array

Known:

I have array of objects like this

[{jam: 3, jumlah: 1000},{jam: 4, jumlah: 1200},{jam: 2, jumlah: 398}]

Wanted:

I want to get the max value from jam

Question:

How to do it … ?

Answer:


let arrMax = [{jam: 3, jumlah: 1000},{jam: 4, jumlah: 1200},{jam: 2, jumlah: 398}]
var angkaMax = Math.max.apply(Math, arrMax.map(function(objAngka) { return objAngka.jam; }));

Explain:

  • arrMax.map(function(objAngka) { return objAngka.jam; }), this code means to get all jam value from objAngka argument (this argument refer to object inside arrMax array), result = [3, 4, 2]
  • Math.max.apply is the function to get the max value from arrMax Array, result = 4

Thanks for reading

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *