嵌套for循环始终显示最后的数据

时间:2016-08-10 05:19:50

标签: javascript arrays for-loop closures

你有一个javascript代码:

var outerSet = [{date: '2016-12-12:00', value: 50}, {date: '2016-12-13:00', value: 50},
                {date: '2016-12-14:00', value: 70}, {date: '2016-12-15:00', value: 50}];
    
var hourlyData = ['2016-12-13:01','2016-12-13:02','2016-12-13:03',
                  '2016-12-13:04','2016-12-13:05']

var iterator = 0;
var tempData = [];

for (var i = 0; i < outerSet.length; i++) {

  var innerItem = outerSet[i];

  if (innerItem.date === '2016-12-13:00') {

    for (var j = 0; j < 4; j++) {
      tempData[iterator] = innerItem;
      tempData[iterator].date = hourlyData[j];
      iterator++;
    }
  }
}
console.log('temp data', tempData);

我希望新的tempData数组的日期值具有类似

的值
[{date: '2016-12-12:01', value: 50},
 {date: '2016-12-13:02', value: 50},
 {date: '2016-12-14:03', value: 70},
 {date: '2016-12-15:04', value: 50}];

但它总是让我回来

[{date: '2016-12-12:04', value: 50},
 {date: '2016-12-13:04', value: 50},
 {date: '2016-12-14:04', value: 70},
 {date: '2016-12-15:04', value: 50}];

这可能是一个封闭问题,但我无法弄清楚这个问题。

以下是相同的小提琴:https://jsfiddle.net/nck66sre/

4 个答案:

答案 0 :(得分:1)

问题在于这一行:

tempData

在每次迭代中,您将当前tempData[iterator] = { date: hourlyData[j], value: innerItem.value }; 数组元素指定为相同的对象。因此,当循环结束时,数组中的所有四个元素都引用相同的对象。 (所以实际上你的代码并没有输出你所说的。)

您需要在每次迭代时创建一个新对象:

var outerSet = [{date: '2016-12-12:00', value: 50}, {date: '2016-12-13:00', value: 50},
                {date: '2016-12-14:00', value: 70}, {date: '2016-12-15:00', value: 50}];
    
var hourlyData = ['2016-12-13:01','2016-12-13:02','2016-12-13:03',
                  '2016-12-13:04','2016-12-13:05']

var tempData = [];

for (var i = 0; i < outerSet.length; i++) {

  var innerItem = outerSet[i];

  if (innerItem.date === '2016-12-13:00') {

    for (var j = 0; j < 4; j++) {
      tempData.push({ date: hourlyData[j], value: innerItem.value });
    }
  }
}
console.log('temp data', tempData);

在上下文中:

&#13;
&#13;
.push()
&#13;
&#13;
&#13;

(注意:我已经使用了iterator并取消了if (strcasecmp ($toSearch,$textData[$i])==0){ //case insensitive comparison echo "Yes"; } 变量,但这仅仅是为了整理,它与之无关你的问题。)

答案 1 :(得分:0)

您需要根据您想要的内容更改条件。请参考此代码。

\1

答案 2 :(得分:0)

var outerSet = [{date: '2016-12-12:00', value: 50}, {date: '2016-12-13:00', value: 50},{date: '2016-12-14:00', value: 70},{date: '2016-12-15:00', value: 50}];

var hourlyData = ['2016-12-13:01','2016-12-13:02','2016-12-13:03','2016-12-13:04','2016-12-13:05']

      var iterator = 0;

      var tempData = [];

      for (var i=0; i<outerSet.length; i++) {

        var innerItem =  outerSet[i];

        if (innerItem.date === '2016-12-13:00') {

          for (var j = 0; j < 4; j++) {
             //remember object get value by refference and you need create new object then add value for object property
              tempData[iterator] = {};

              tempData[iterator].value = innerItem.value;

              tempData[iterator].date = hourlyData[j];
              iterator++;
          }
        }
      }
      console.log(JSON.stringify(tempData));

答案 3 :(得分:0)

类似传递指针

英文翻译:所有指针都指向同一个对象

function createNew(date,value){
    var obj ={
        "date":date,
        "value":value
    };
    return obj;
}

var outerSet = [{date: '2016-12-12:00', value: 40}, {date: '2016-12-13:00', value: 50},{date: '2016-12-14:00', value: 70},{date: '2016-12-15:00', value: 60}];
var hourlyData = ['2016-12-13:10','2016-12-13:20','2016-12-13:30','2016-12-13:40','2016-12-13:50'];

var tempData = new Array();

for (var i=0; i<outerSet.length; i++) {
    var innerItem =  outerSet[i];
    if (innerItem.date == '2016-12-13:00') {
        for (var j = 0; j < 4; j++) {
            var tempOuter=createNew(hourlyData[j],innerItem.value);
            tempData.push(tempOuter);
        }
    }
}
console.log('temp data', tempData);