为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁华开满天机 2019-05-21 09:25:41
有两组数组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":"广东"}];
查看完整描述

2 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

你改变的是对象的引用地址所有最后改变的影响前面push进去的了
varnewArr=[];
arr01.forEach(v1=>{
arr02.forEach(v2=>{
newArr.push(Object.assign({},v2,{
account:v1.account
}))
})
})
console.log(newArr)
                            
查看完整回答
反对 回复 2019-05-21
  • 2 回答
  • 0 关注
  • 1124 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信