react+d3 | problem 2(或与作用域相关)

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);

输出结果错误
react+d3 | problem 2(或与作用域相关)

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);

输出结果正确
react+d3 | problem 2(或与作用域相关)

上一篇:[MEAN Stack] First API -- 7. Using Route Files to Structure Server Side API


下一篇:[CF1513F]Swapping Problem