0

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.

1
  • That's a syntax error; keys and values in a JavaScript object should be associated/assigned with {keyName : keyValue}. Commented Nov 25, 2014 at 13:23

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you this is exactly what I was looking for! My goal was to get the top position of a polygon in google maps. All I needed to change was get the highest value of k instead of B but I managed to change that thanks a lot!
A better way would be to use a LatLngBounds() object, extend it by adding all paths of the polygon, and call the .getBounds() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.