map循环嵌套,总是取得外层数组的最后一个值

有两组数组arr01和arr02现在下把arr01数组中的某个字段添加到arr02中,现在问题是最后得到的新数组总是arr01的最后一个字段
letarr01=[
{
"account":"000000000000"
},
{
"account":"111111111111"
},
{
"account":"2222222222222"
}
];
letarr02=[
{
"city":"北京"
},
{
"city":"上海"
},
{
"city":"广东"
}
];
letnewArr=[];
arr01.map((cur01,eq)=>{
arr01.map((cur02,index)=>{
cur02.account=cur01.account;
returnnewArr.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":"广东"
}
];
繁华开满天机
浏览 1125回答 2
2回答

一只名叫tom的猫

你改变的是对象的引用地址所有最后改变的影响前面push进去的了varnewArr=[];arr01.forEach(v1=>{arr02.forEach(v2=>{newArr.push(Object.assign({},v2,{account:v1.account}))})})console.log(newArr)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript