岩崎理奈
2020-07-16 14:25
采纳率: 25%
浏览 82

有三层数组怎么和在一起?急急急 求大神zhi'dian

let arry = [
{name:'张三',
age:18,
list:[  
       {num:21,list1[{money:1,money2:',money3:4}]}
       {num:21,list1[{money:2,money2:'5',money3:''}]}
    ]
}
{name:'里斯',
age:19,
list:[
       {num:22,list1[{money:1,money2:''money3:''}]}
       {num:22,list1[{money:3,money2:'8',money3:10}]}
    ]
}
]

要求得到如下结果:
[
{name:'张三',age:18,money:1,money2:'',money3:4}
{name:'张三',age:18,money:2,money2:'5',money3:''}
{name:'里斯',age:19,money:1,money2:'',money3:''}
{name:'里斯',age:19,money:3,money2:'8',money3:10}
]
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 超小少 2020-07-29 14:14
    已采纳
     //原数组
             let arr = [
                {
                   name: '张三',
                   age: 18,
                   list: [
                      { num: 21, list1: [{ money: 1, money2: 6, money3: 4 }] },
                      { num: 21, list1: [{ money: 2, money2: '5', money3: '' }] }
                   ]
                },
                {
                   name: '里斯',
                   age: 19,
                   list: [
                      { num: 22, list1: [{ money: 1, money2: '', money3: '' }] },
                      { num: 22, list1: [{ money: 3, money2: '8', money3: 10 }] }
                   ]
                }
             ];
            //得到的新数组
             let arr2 = [];
             arr.forEach((item, index) => {
                for (let i in item.list) {
                   let obj = {}
                   obj['name'] = item.name;
                   obj['age'] = item.age;
                   obj = Object.assign(obj, ...item.list[i].list1);
                   arr2.push(obj)
                }
             });
             console.log(arr2)
    

    图片说明

    点赞 打赏 评论

相关推荐 更多相似问题