开发者

Object prototype being included in array [closed]

开发者 https://www.devze.com 2023-04-08 14:33 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

I'm working on a function that pulls the lat/lng from an MVCArray of markers. The function is below. However, each object in the mapData array contains the prototype of the native object, along with lat/lng. I have played around with hasOwnProperty, but without much luck. Am I doing something obviously wrong here?

    function prepareMarkers() {
    var mapData = [];

    // All we need from our markers list is the co开发者_开发技巧ordinates of the marker and title if it has one
    markers.forEach(function(elem, index) {
        mapData.push({
            lat: elem.getPosition().lat(),
            lng: elem.getPosition().lng()
        });
    });

    return mapData;
}


You are putting native objects into the mapData array. That's what this is. It's an object.

    {
        lat: elem.getPosition().lat(),
        lng: elem.getPosition().lng()
    }

So, it's working exactly as it should. Since they are objects, they should have the methods from the prototype of the native object. Is there a particular problem you are trying to solve here? Everything you've described so far sounds correct.

In your array, you can just access things like this:

var latitude = mapData[0].lat;
var longitude = mapData[0].lng;
0

精彩评论

暂无评论...
验证码 换一张
取 消