function monthData(oriData) {
let mp = {};
let tmpObj = {};
oriData.forEach(d => {
tmpObj.max_temp = d['max_temperature'];
tmpObj.min_temp = d['min_temperature'];
if (mp[d['date'].substring(0, 7)] === undefined) {
mp[d['date'].substring(0, 7)] = [];
mp[d['date'].substring(0, 7)].push(tmpObj);
}
else mp[d['date'].substring(0, 7)].push(tmpObj);
});
return mp;
}
执行:
let monthMap = monthData(rawData);
console.log(monthMap);
输出结果错误
function monthData(oriData) {
let mp = {};
oriData.forEach(d => {
let tmpObj = {
max_temp: d['max_temperature'],
min_temp: d['min_temperature']
};
/* tmpObj.max_temp = d['max_temperature'];
tmpObj.min_temp = d['min_temperature']; */
if (mp[d['date'].substring(0, 7)] === undefined) {
mp[d['date'].substring(0, 7)] = [];
mp[d['date'].substring(0, 7)].push(tmpObj);
}
else mp[d['date'].substring(0, 7)].push(tmpObj);
});
return mp;
}
执行:
let monthMap = monthData(rawData);
console.log(monthMap);
输出结果正确