Sinan Samet
Sinan Samet

Reputation: 6742

Highest value of an array inside an array

I have an array with these values (returned from google.maps.LatLng):

var polygon = [
 { k=50.882917044569744, B=6.003985404968262}, 
 { k=50.88289589175401, B=6.003938466310501}, 
 { k=50.882934389871465, B=6.003895550966263}, 
 { k=50.88295554266969, B=6.003943160176277}
]

And I want the key of the one with the highest B value so in this case it would be polygon[0] I don't know how exactly the array is made actually but console.log() returns me this exactly:

[df { k=50.882917044569744, B=6.003985404968262, toString=function(), meer...}, df { k=50.88289589175401, B=6.003938466310501, toString=function(), meer...}, df { k=50.882934389871465, B=6.003895550966263, toString=function(), meer...}, df { k=50.88295554266969, B=6.003943160176277, toString=function(), meer...}]

And polygon[0] would return me the first object.

Upvotes: 0

Views: 57

Answers (1)

Salman Arshad
Salman Arshad

Reputation: 272006

You can always iterate the array with:

var polygon = [
    {k: 50.882917044569744, B: 6.003985404968262},
    {k: 50.88289589175401,  B: 6.003938466310501}, 
    {k: 50.882934389871465, B: 6.003895550966263},
    {k: 50.88295554266969,  B: 6.003943160176277}
];
var i, p;
for (i = 0; i < polygon.length; i++) {
    if (p === undefined || p.k < polygon[i].k) {
        p = polygon[i];
    }
}
console.log(p);

Upvotes: 1

Related Questions