简体   繁体   English

为什么我不能迭代这个JavaScript对象?

[英]Why cant I iterate over this JavaScript object?

I have this javascript object.. 我有这个javascript对象..

var obj = {
    '02/08/2016': 2,
    '03/10/2016': 4,
    '04/05/2016': 2,
    '04/06/2016': 35,
    '04/19/2016': 4,
    '04/26/2016': 22,
    '05/09/2016': 15,
    '05/24/2016': 2,
    '05/30/2016': 4,
    '07/14/2016': 7,
    '08/18/2016': 200
};

// Does not work
$(obj).each(function(index,value) {
  console.log(index);
  console.log(value);
});

// Does not work, also what does putting it in bracket notation do here?
var labels = $.map(obj, function(index, value) { 
   return [index];  
});

Why can I not iterate the object? 为什么我不能迭代对象? I am trying to place this data in two separate arrays (like below) for chart.js 我试图将这些数据放在chart.js的两个独立数组(如下所示)中

var arr1 = ['02/08/2016', '03/10/2016', '04/05/2016', ..];
var arr2 = [2, 4, 2, ...];

Code Fiddle: https://jsfiddle.net/zjgb6ez4/ 代码小提琴: https//jsfiddle.net/zjgb6ez4/

The issue with your logic is, $.each() has two signatures: 您的逻辑问题是, $.each()有两个签名:

$(selector).each(function(index,value) {...} // For HTML DOM selectors.
$.each(obj, function(index,value) {...}      // For JavaScript Objects & Arrays.

What you have used is for jQuery selectors, DOM Iteration. 您使用的是jQuery选择器,DOM迭代。 This way is specifically for iterating JavaScript Objects or Arrays. 这种方式专门用于迭代JavaScript对象或数组。

Also, since you need two arrays. 此外,因为你需要两个数组。 You can't use each or map function for this, as they return only one array. 您不能使用eachmap函数,因为它们只返回一个数组。 Instead, it is better to use Object.keys and Object.values() : 相反,最好使用Object.keysObject.values()

 var obj = { '02/08/2016': 2, '03/10/2016': 4, '04/05/2016': 2, '04/06/2016': 35, '04/19/2016': 4, '04/26/2016': 22, '05/09/2016': 15, '05/24/2016': 2, '05/30/2016': 4, '07/14/2016': 7, '08/18/2016': 200 }; var arr1 = Object.keys(obj); var arr2 = Object.values(obj); console.log(arr1); console.log(arr2); 

Note: Object.values() is an experimental technology. 注意: Object.values()是一种实验技术。 Because this technology's specification has not stabilised, check the compatibility table for usage in various browsers. 由于此技术的规范尚未稳定,请检查兼容性表以了解各种浏览器的用法。 Also note that the syntax and behaviour of an experimental technology is subject to change in future versions of browsers as the specification changes. 另请注意,随着规范的更改,实验技术的语法和行为可能会在未来版本的浏览器中发生变化。

Without Object.values() 没有Object.values()

 var obj = { '02/08/2016': 2, '03/10/2016': 4, '04/05/2016': 2, '04/06/2016': 35, '04/19/2016': 4, '04/26/2016': 22, '05/09/2016': 15, '05/24/2016': 2, '05/30/2016': 4, '07/14/2016': 7, '08/18/2016': 200 }; var arr1 = Object.keys(obj); var arr2 = arr1.map(function (v) { return obj[v]; }); console.log(arr1); console.log(arr2); 

Using jQuery to iterate over a plain object you need to use $.each() . 使用jQuery迭代普通对象,您需要使用$.each()

$.each(obj, function(index,value) {...}

A pure Javascript solution might be: 纯Javascript解决方案可能是:

for (var index in obj) {
    console.log(index);
    console.log(obj[index]);
}

You can use the keys() and values() methods on Object . 您可以在Object上使用keys()values()方法。

var obj = {
    '02/08/2016': 2,
    '03/10/2016': 4,
    '04/05/2016': 2,
    '04/06/2016': 35,
    '04/19/2016': 4,
    '04/26/2016': 22,
    '05/09/2016': 15,
    '05/24/2016': 2,
    '05/30/2016': 4,
    '07/14/2016': 7,
    '08/18/2016': 200
};
var arr1 = Object.keys(obj);
var arr2 = Object.values(obj);

This is plain JavaScript solution, without jQuery. 这是一个简单的JavaScript解决方案,没有jQuery。 But note that values() method is not supported in all browsers yet. 但请注意,所有浏览器都不支持values()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM