我在图表系统上工作,我正在从ajax json响应中获取数据。我只是试图计算某些JSON对象并将它们分组。
例如,我试图循环每个JSON对象并将所有颜色(红色,蓝色,黄色)分成组,在这些组中我想按月分割结果。然后我可以抓住结果并将其放入我的图表。
我目前收到错误:
Cannot set property '2016-10' of undefined
这是我试图完成的一个例子。 https://jsfiddle.net/awo5aaqb/5/
以下是导致我出现问题的代码:
var dateCounts_Red = {};
var dateCounts_Blue = {};
var dateCounts_Yellow = {};
data.d.results.forEach(function(element) {
var date = element.created_date.slice(0, 7);
var yr = date.slice(0, 4);
var Color = element.colorvalue;
if (Color === "Red") {
if (!dateCounts_Red.hasOwnProperty(yr)) {
dateCounts_Red[yr] = {}
}
if (!dateCounts_Red[yr].hasOwnProperty(date)) {
dateCounts_Red[yr][date] = 0
}
dateCounts_Red[yr][date]++;
}else {dateCounts_Red[yr][date] = 0}
if (Color === "Blue") {
if (!dateCounts_Blue.hasOwnProperty(yr)) {
dateCounts_Blue[yr] = {}
}
if (!dateCounts_Blue[yr].hasOwnProperty(date)) {
dateCounts_Blue[yr][date] = 0
}
dateCounts_Blue[yr][date]++;
}else {dateCounts_Blue[yr][date] = 0}
if (Color === "Yellow") {
if (!dateCounts_Yellow.hasOwnProperty(yr)) {
dateCounts_Yellow[yr] = {}
}
if (!dateCounts_Yellow[yr].hasOwnProperty(date)) {
dateCounts_Yellow[yr][date] = 0
}
dateCounts_Yellow[yr][date]++;
}else {dateCounts_Yellow[yr][date] = 0}
});
Red_yr_2015_data = [dateCounts_Red['2015']['2015-01'],dateCounts_Red['2015']['2015-02'],dateCounts_Red['2015']['2015-03'],dateCounts_Red['2015']['2015-04'],dateCounts_Red['2015']['2015-05'],dateCounts_Red['2015']['2015-06'],dateCounts_Red['2015']['2015-07'],dateCounts_Red['2015']['2015-08'],dateCounts_Red['2015']['2015-09'],dateCounts_Red['2015']['2015-10'],dateCounts_Red['2015']['2015-11'],dateCounts_Red['2015']['2015-12']];
Blue_yr_2015_data = [dateCounts_Blue['2015']['2015-01'],dateCounts_Blue['2015']['2015-02'],dateCounts_Blue['2015']['2015-03'],dateCounts_Blue['2015']['2015-04'],dateCounts_Blue['2015']['2015-05'],dateCounts_Blue['2015']['2015-06'],dateCounts_Blue['2015']['2015-07'],dateCounts_Blue['2015']['2015-08'],dateCounts_Blue['2015']['2015-09'],dateCounts_Blue['2015']['2015-10'],dateCounts_Blue['2015']['2015-11'],dateCounts_Blue['2015']['2015-12']];
Yellow_yr_2015_data = [dateCounts_Yellow['2015']['2015-01'],dateCounts_Yellow['2015']['2015-02'],dateCounts_Yellow['2015']['2015-03'],dateCounts_Yellow['2015']['2015-04'],dateCounts_Yellow['2015']['2015-05'],dateCounts_Yellow['2015']['2015-06'],dateCounts_Yellow['2015']['2015-07'],dateCounts_Yellow['2015']['2015-08'],dateCounts_Yellow['2015']['2015-09'],dateCounts_Yellow['2015']['2015-10'],dateCounts_Yellow['2015']['2015-11'],dateCounts_Yellow['2015']['2015-12']];
Red_yr_2016_data = [dateCounts_Red['2016']['2016-01'],dateCounts_Red['2016']['2016-02'],dateCounts_Red['2016']['2016-03'],dateCounts_Red['2016']['2016-04'],dateCounts_Red['2016']['2016-05'],dateCounts_Red['2016']['2016-06'],dateCounts_Red['2016']['2016-07'],dateCounts_Red['2016']['2016-08'],dateCounts_Red['2016']['2016-09'],dateCounts_Red['2016']['2016-10'],dateCounts_Red['2016']['2016-11'],dateCounts_Red['2016']['2016-12']];
Blue_yr_2016_data = [dateCounts_Blue['2016']['2016-01'],dateCounts_Blue['2016']['2016-02'],dateCounts_Blue['2016']['2016-03'],dateCounts_Blue['2016']['2016-04'],dateCounts_Blue['2016']['2016-05'],dateCounts_Blue['2016']['2016-06'],dateCounts_Blue['2016']['2016-07'],dateCounts_Blue['2016']['2016-08'],dateCounts_Blue['2016']['2016-09'],dateCounts_Blue['2016']['2016-10'],dateCounts_Blue['2016']['2016-11'],dateCounts_Blue['2016']['2016-12']];
Yellow_yr_2016_data = [dateCounts_Yellow['2016']['2016-01'],dateCounts_Yellow['2016']['2016-02'],dateCounts_Yellow['2016']['2016-03'],dateCounts_Yellow['2016']['2016-04'],dateCounts_Yellow['2016']['2016-05'],dateCounts_Yellow['2016']['2016-06'],dateCounts_Yellow['2016']['2016-07'],dateCounts_Yellow['2016']['2016-08'],dateCounts_Yellow['2016']['2016-09'],dateCounts_Yellow['2016']['2016-10'],dateCounts_Yellow['2016']['2016-11'],dateCounts_Yellow['2016']['2016-12']];
我知道目前代码非常笨重,但有人会知道对此更好的方法吗?
答案 0 :(得分:1)
不要重新发明轮子,而是尝试以这种方式绕过问题:
按对象的属性对对象进行分组是一项常见任务。有许多图书馆可以帮助解决这个问题。
groupBy(来自下划线网站的文档)
将集合拆分为集合,按运行每个集合的结果进行分组 通过iteratee的价值。如果iteratee是一个字符串而不是一个函数, 由iteratee在每个值上命名的属性组。
_.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
=> {1: [1.3], 2: [2.1, 2.4]}
_.groupBy(['one', 'two', 'three'], 'length');
=> {3: ["one", "two"], 5: ["three"]}
What is the most efficient method to groupby on a javascript array of objects?
答案 1 :(得分:1)
它失败了,因为如果颜色不是红色,它会转到else并尝试设置对象,但是没有创建对象。
if (Color === "Red") {
if (!dateCounts_Red.hasOwnProperty(yr)) { <-- you create it here
dateCounts_Red[yr] = {}
}
if (!dateCounts_Red[yr].hasOwnProperty(date)) {
dateCounts_Red[yr][date] = 0
}
dateCounts_Red[yr][date]++;
}else {dateCounts_Red[yr][date] = 0} <-- it needed to be set here too...
所以移动第一个hasOwnProperty检查if之外的if和else都需要它。
答案 2 :(得分:0)
如果你想轻松出路
if (Color === "Red") {
if (!dateCounts_Red.hasOwnProperty(yr)) { <-- you create it here
dateCounts_Red[yr] = {}
}
if (!dateCounts_Red[yr].hasOwnProperty(date)) {
dateCounts_Red[yr][date] = 0
}
dateCounts_Red[yr][date]++;
}else {dateCounts_Red[yr]={date:0}} // set your object here, too