假设有2个数组,结构大致是这样的[{code:'A',list:[{name:'A'},{name:'B'},{name:'C'}]},{code:'D',list:[{name:'A'},{name:'B'},{name:'C'}]}];[{code:'A',list:[{name:'A'},{name:'B'},{name:'C'},{name:'D'}]},{code:'B',list:[{name:'A'},{name:'B'}]}];然后先判断2个数组中的code是否相等,如果相等在遍历里面的list在判断name是否相等,有什么优雅的解决方案,目前嵌套了好几层,实现是可以实现,但是感觉不优雅,想求个优雅的方案洗洗脑。
2 回答
拉莫斯之舞
TA贡献1820条经验 获得超10个赞
leta=[{name:'A'},{name:'B'},{name:'C'}]letb=[{name:'A'},{name:'B'},{name:'C'},{name:'D'}]a.push(...b)a.filter((v,index)=>index===a.findIndex(y=>y.name===v.name))试试这样?
添加回答
举报
0/150
提交
取消