对象遍历特定点

时间:2014-12-22 11:29:35

标签: javascript javascript-objects

我正在我的代码中预加载图片。我想只遍历前五个对象,我该怎么做?

我可以从某个特定的点长度开始遍历Object吗?

http://jsfiddle.net/pvuvkzgt/

代码:

for (var key in detailes_obj) {
    if (detailes_obj.hasOwnProperty(key)) {
        // do something with albums[key]
        console.log(detailes_obj[key].poster);
    }
}

1 个答案:

答案 0 :(得分:-1)

我担心你需要在for循环之外创建一个count变量,如下所示:

var count = 0;
for (var key in detailes_obj) {
    if (detailes_obj.hasOwnProperty(key) && count < 5) {
        // do something with albums[key]
        console.log(detailes_obj[key].poster);
        count++;
    }
}

Updated your fiddle as well