有两组数组arr01和arr02
现在下把arr01数组中的某个字段添加到arr02中,现在问题 是最后得到的新数组总是arr01的最后一个字段
let arr01 = [
{
"account" : "000000000000"
},
{
"account" : "111111111111"
},
{
"account" : "2222222222222"
}
];
let arr02 = [
{
"city" : "北京"
},
{
"city" : "上海"
},
{
"city" : "广东"
}
];
let newArr = [];
arr01.map((cur01,eq) => {
arr01.map((cur02,index) => {
cur02.account = cur01.account;
return newArr.push(cur);
})
})
最后结果:
newArr = [
{
"account" : "2222222222222",
"city" : "北京"
},
{
"account" : "2222222222222",
"city" : "上海"
},
{
"account" : "2222222222222",
"city" : "广东"
},
{
"account" : "2222222222222",
"city" : "北京"
},
{
"account" : "2222222222222",
"city" : "上海"
},
{
"account" : "2222222222222",
"city" : "广东"
},
{
"account" : "2222222222222",
"city" : "北京"
},
{
"account" : "2222222222222",
"city" : "上海"
},
{
"account" : "2222222222222",
"city" : "广东"
}
];
我想得到的结果是:
newArr = [
{
"account" : "000000000000",
"city" : "北京"
},
{
"account" : "000000000000",
"city" : "上海"
},
{
"account" : "000000000000",
"city" : "广东"
},
{
"account" : "111111111111",
"city" : "北京"
},
{
"account" : "111111111111",
"city" : "上海"
},
{
"account" : "111111111111",
"city" : "广东"
},
{
"account" : "2222222222222",
"city" : "北京"
},
{
"account" : "2222222222222",
"city" : "上海"
},
{
"account" : "2222222222222",
"city" : "广东"
}
];
你改变的是对象的引用地址 所有最后改变的影响前面push进去的了