如何在不合并对象属性的情况下推送对象数组以创建新数组?

时间:2020-06-07 14:50:03

标签: javascript arrays push

我正在尝试将循环输出保存到一个数组中。 如果我console.log(arr2)我得到了想要的结果,但是得到了多个数组。我想要所有这些放在一个阵列中。 当我尝试使用.push时,所有数组的结果都相同。

也许我可以用其他方式做到这一点,但我不知道如何做到。

    let b = {};
    let arr3 = [];
    let arr1 = [{value1: 123, value2: 34, value3: 90, value4: 23, value5: 32},{value1: 10, value2: 09, value3: 54, value4: 32, value5: 311}];
    for (let a = 0; a <=4; a++){
       b = {"value1": a*a, "value3": a+12, "value4": a/2};
       arr2 = arr1.map(function(record){
       record.value1 = b.value1;
       record.value3 = b.value3;
       record.value4 = b.value4;
       return record;
       })
       // console.log(arr2);
       arr3.push(arr2);

}
console.log(arr3);

这是我用.push完成的,这不是我想要的。

  [
  { value1: 0, value2: 34, value3: 12, value4: 0, value5: 32 },
  { value1: 0, value2: 9, value3: 12, value4: 0, value5: 311 }
]
[
  { value1: 1, value2: 34, value3: 13, value4: 0.5, value5: 32 },
  { value1: 1, value2: 9, value3: 13, value4: 0.5, value5: 311 }
]
[
  { value1: 4, value2: 34, value3: 14, value4: 1, value5: 32 },
  { value1: 4, value2: 9, value3: 14, value4: 1, value5: 311 }
]
[
  { value1: 9, value2: 34, value3: 15, value4: 1.5, value5: 32 },
  { value1: 9, value2: 9, value3: 15, value4: 1.5, value5: 311 }
]
[
  { value1: 16, value2: 34, value3: 16, value4: 2, value5: 32 },
  { value1: 16, value2: 9, value3: 16, value4: 2, value5: 311 }
]

当我在for循环中进行console.log(arr2)时,我几乎得到了所需的结果,但所需的结果却不在单个数组中。

   [
  { value1: 0, value2: 34, value3: 12, value4: 0, value5: 32 },
  { value1: 0, value2: 9, value3: 12, value4: 0, value5: 311 }
]
[
  { value1: 1, value2: 34, value3: 13, value4: 0.5, value5: 32 },
  { value1: 1, value2: 9, value3: 13, value4: 0.5, value5: 311 }
]
[
  { value1: 4, value2: 34, value3: 14, value4: 1, value5: 32 },
  { value1: 4, value2: 9, value3: 14, value4: 1, value5: 311 }
]
[
  { value1: 9, value2: 34, value3: 15, value4: 1.5, value5: 32 },
  { value1: 9, value2: 9, value3: 15, value4: 1.5, value5: 311 }
]
[
  { value1: 16, value2: 34, value3: 16, value4: 2, value5: 32 },
  { value1: 16, value2: 9, value3: 16, value4: 2, value5: 311 }
]

预期结果:

[
  { value1: 0, value2: 34, value3: 12, value4: 0, value5: 32 },
  { value1: 0, value2: 9, value3: 12, value4: 0, value5: 311 },
  { value1: 1, value2: 34, value3: 13, value4: 0.5, value5: 32 },
  { value1: 1, value2: 9, value3: 13, value4: 0.5, value5: 311 },
  { value1: 4, value2: 34, value3: 14, value4: 1, value5: 32 },
  { value1: 4, value2: 9, value3: 14, value4: 1, value5: 311 },
  { value1: 9, value2: 34, value3: 15, value4: 1.5, value5: 32 },
  { value1: 9, value2: 9, value3: 15, value4: 1.5, value5: 311 },
  { value1: 16, value2: 34, value3: 16, value4: 2, value5: 32 },
  { value1: 16, value2: 9, value3: 16, value4: 2, value5: 311 }
]

1 个答案:

答案 0 :(得分:0)

这是因为对象通过引用来工作。这应该工作: 替换为:

 arr2 = arr1.map(function(record){
   record.value1 = b.value1;
   record.value3 = b.value3;
   record.value4 = b.value4;
   return record;
   })

通过您的代码进行此操作:

 arr2 = arr1.map(function(record) {
const tempRecord = {...record}
tempRecord.value1 = b.value1;
tempRecord.value3 = b.value3;
tempRecord.value4 = b.value4;
return tempRecord;
})

您可以通过执行以下操作来获得最终结果: arr3.flat();