jQuery-遍历数据存储对象

jQuery-遍历数据存储对象,jquery,Jquery,我在尝试迭代存储的对象的属性时遇到了一些非常奇怪的事情 使用jQuery数据函数 下面是一个例子: 然后,我尝试使用以下方法读取值: $.each( wrapper.data('infos'), function(k,v) { console.log(k + ' > ' + v); }); 我得到了一个漂亮的输出,比如: 0 > undefined 1 > undefined ... 239 > undefined 如果我像这样输出这个对象,我可以毫无困难地读取

我在尝试迭代存储的对象的属性时遇到了一些非常奇怪的事情 使用jQuery数据函数

下面是一个例子:

然后,我尝试使用以下方法读取值:

$.each( wrapper.data('infos'), function(k,v) {
  console.log(k + ' > ' + v);
});
我得到了一个漂亮的输出,比如:

0 > undefined
1 > undefined
... 
239 > undefined
如果我像这样输出这个对象,我可以毫无困难地读取属性。
它是否与jquery缓存或其他相关

因为您的对象具有
length
属性,所以它被解释为数组,至少在1.7.2中,它是否是数组取决于:

length = obj.length,            
isObj = length === undefined || jQuery.isFunction( obj );
所以你要么

  • 将您的
    length
    属性称为其他属性
  • 改用
    for/in
    循环

    var data = wrapper.data('infos')
    
    for (var x in data) {
        if (data.hasOwnProperty(x)) { // omit properties from the prototype chain
            console.log(x + ' > ' + data[x]);
        }
    }
    

  • 您可以将console.log(wrapper.data('infos')的输出放在pastebin或JSFIDLE中吗?您使用的是哪个版本的jQuery?这对我来说就像预期的一样(jquery1.7.2)。我也会考虑包括一个显示问题的jsFoDLE。检查一下。“Matt:我使用jQuery 1.7.2,我宁愿使用for/in循环,它在我的情况下是完美的。非常感谢,你真的帮助了我!
    var data = wrapper.data('infos')
    
    for (var x in data) {
        if (data.hasOwnProperty(x)) { // omit properties from the prototype chain
            console.log(x + ' > ' + data[x]);
        }
    }