0

我正在尝试像遍历数组一样遍历对象。我正在努力将循环计数器附加到变量名。

我有一个像这样的对象(输出dump(),我在这里找到):

object(2): {
  elem0:  array(4): {
    [0]:  string(27): "http://placehold.it/300x300"
    [1]:  string(3): "0.8"
    [2]:  string(4): "-150"
    [3]:  string(3): "200"
  }
  elem1:  array(4): {
    [0]:  string(27): "http://placehold.it/300x300"
    [1]:  string(3): "0.6"
    [2]:  string(3): "-70"
    [3]:  string(3): "458"
  }
}

这是我尝试循环的方式:

jQuery(document).ready(function($) {

    // Provides object-measuring functionality
    Object.size = function(obj) {
        var size = 0, key;
        for (key in obj) {
            if (obj.hasOwnProperty(key)) size++;
        }
        return size;
    };

    // Returns the number of objects in my object
    var size = Object.size(window.depthElems);

    /*
    This is where I'm having difficulty.
    I would like to use window.depthElems.elem0,
    then window.depthElems.elem1, etc.
    */

    for (var i = 0; i < size; i++) {
        $('.wrapper').append('<img src="' + window.depthElems.elem+i+[0] + '" />'); 
    }

});
4

2 回答 2

3

为了争论,我也会提供我的问题作为答案。您可以使用:

for(element in window.depthElems) {
    if(window.depthElems.hasOwnProperty(element)) {
        $('.wrapper').append('<img src="' + window.depthElems[element] + '" />');
    }
}

这不仅更优雅,而且需要的代码也少得多。当然,如果有理由使用其他代码,请说出来。

注意:此代码经过编辑,还包括读取“数组”的功能,但问题是使其与“对象”一起使用。如果您使用“对象”,则“hasOwnProperty”检查是多余的。

注意#2:您也可以var hasOwn = Object.prototype.hasOwnProperty;Azder所说的那样使用,这是一个很好的保障措施。

于 2012-07-25T18:14:51.097 回答
2

如果我的回答太过分了,我深表歉意,我只是想防止误用 JS(我经历过很多)进一步伤害。

jQuery(document).ready(function($) {

    var i; // there is no block scope in JS, so better to be clear and define i here
    var $wrapper; // also

    // Changing the JS built-in objects is problematic most of the time
    // You should learn from jQuery and do wrapping instead
    // Or at least just a good namespasing like:
    // MyFramework.objectSize = function (obj) {}

    Object.size = function(obj) {
        var size = 0, key;
        var hasOwn = Object.prototype.hasOwnProperty; // will explain further down
        for (key in obj) {
            // if obj has redifined hasOwnProperty = function(){ return false; }?
            // it's better to use hasOwn like this if(hasOwn.call(obj,key)) {}
            // and please do use braces even if only 1 statement
            if(hasOwn.call(obj,key)) size++;
        }
        return size;
    };

    // Returns the number of objects in my JSON object
    var size = Object.size(window.depthElems);

    $wrapper = $('.wrapper'); // cached so jQuery doesn't search for it each iteration    

    // i is scoped to the whole function anyways
    for (i = 0; i < size; i++) {

        // $.each even guards you of the changing DOM which can cause
        // infinite loops (you don't have that problem here, but... good to know
        $.each(window['depthElems'+i],function(index,element){
            $wrapper.append('<img src="' + element + '" />');
        }); 
    }

});

此外,由于您已经创建了名为 elem1、elem2、elem3... 的对象,您不妨使用二维数组,例如window.depthElems = [[],[],[]]

于 2012-07-25T18:39:28.447 回答