Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find largest id within array object using $.each

[{"id":1},{"id":2},{"id":3}]

I know I can use max like this var largest = Math.max.apply(Math, myArray) (if I have my array like this [1,2,3]) but since I have to loop through a list, I just wonder I can use loop to get the largest number;

$.each(function(){
//this.id
// how to continue here?
});
like image 481
Alice Xu Avatar asked Feb 10 '23 10:02

Alice Xu


1 Answers

You can still use the Math.max.apply construct. Simply use map to make an array of ids from the objects:

var maxId = Math.max.apply(Math, myList.map(function(o){ return o.id }));
like image 135
Denys Séguret Avatar answered Feb 12 '23 00:02

Denys Séguret