循环对象项

时间:2016-01-17 01:09:05

标签: javascript loops object

我有非常复杂的对象,我需要遍历多个级别的项目。这很棘手,我现在卡住了。

我尝试了什么(请注意,我添加了一个错误)

var stuffCount = Object.keys(stuff).length;

//Loop through initial items in object
for( i = 0; i < stuffCount; i++ ) {

    // Error occurs on this line:
    // Uncaught TypeError: Cannot read property 'points' of undefined
    var pointCount = stuff[i].points.length; 

    var coordinates = [];

    //Loop through points in initial items
    for( r = 0; r < pointCount; r++ ) {

        coordinates.push(new google.maps.LatLng( 
            stuff[i].points[r].lat, 
            stuff[i].points[r].lng ));
    }

}

对象:它更复杂,但为了这个问题我尽可能简单。

var stuff = {

    first: {

        center: {
            lat: 11,
            lng: 22
        },
        points: [
            {
                lat: 11,
                lng: 22
            },
            {
                lat: 33,
                lng: 44
            },
            {
                lat: 55,
                lng: 66
            },
        ]
    },

    second: {

        center: {
            lat: 11,
            lng: 22
        },
        points: [
            {
                lat: 11,
                lng: 22
            },
            {
                lat: 33,
                lng: 44
            },
            {
                lat: 55,
                lng: 66
            },
        ]
    },

    third: {

        center: {
            lat: 11,
            lng: 22
        },
        points: [
            {
                lat: 11,
                lng: 22
            },
            {
                lat: 33,
                lng: 44
            },
            {
                lat: 55,
                lng: 66
            },
        ]
    },
}

我试图通过使用循环来减少代码。它用于在Google地图上显示多边形。我可以一个接一个地做,但代码会很大,因为我有100个 + 我希望它是动态的,以便我可以轻松添加新的多边形,而不会改变很多代码。

我的代码出了什么问题?

6 个答案:

答案 0 :(得分:1)

问题是您正在尝试使用编号索引从您的stuff对象中检索键。实际上你不需要计数键,使用for...in循环,如下所示:

//Loop through initial items in object
for(var i in stuff) {

    // Error occurs on this line:
    // Uncaught TypeError: Cannot read property 'points' of undefined
    var pointCount = stuff[i].points.length; 

    var coordinates = [];

    //Loop through points in initial items
    for( r = 0; r < pointCount; r++ ) {

        coordinates.push(new google.maps.LatLng( 
            stuff[i].points[r].lat, 
            stuff[i].points[r].lng ));
    }

}

答案 1 :(得分:0)

你对钥匙感到困惑。你应该这样做:

var stuffKeys = Object.keys(stuff);

//Loop through initial items in object
for( i = 0; i < stuffKeys.length; i++ ) {
    var pointCount = stuff[stuffKeys[i]].points.length; 

    var coordinates = [];

    //Loop through points in initial items
    for( r = 0; r < pointCount; r++ ) {

        coordinates.push(new google.maps.LatLng( 
            stuff[stuffKeys[i]].points[r].lat, 
            stuff[stuffKeys[i]].points[r].lng ));
    }
}

你原来要求的是stuff[0] ......这是不存在的。您需要stuff[the first key in stuff] ...

答案 2 :(得分:0)

您要做的是从第一个点开始获取点属性,所以stuff['first'].points

但相反,你所做的是stuff[0].points,而0不是关键。

这是非优化的,但应该传达您需要做的事情:

stuff[Object.keys(stuff)[0]].points

答案 3 :(得分:0)

您应该使用for in来迭代对象属性。 for可用于在数组类型属性中进行迭代。

答案 4 :(得分:0)

您的代码有什么问题?您正在迭代对象键,就像它是一个数组一样

stuff['first']stuff[0]

不同

你怎么能管理这个?我会使用lodash来迭代这样:

_.each(stuff, function(item,name)
{
    _.each(item.points, function(point)
    {
        // do whatever
    });
});

答案 5 :(得分:0)

您应该使用for in循环来遍历对象。见下面的例子:

var stuffCount = Object.keys(stuff).length; //you don't need this

for (var obj in stuff) {

    var pointCount = stuff[obj].hasOwnProperty("points") ? stuff[obj].points.length : null;
    console.log(pointCount);

}