猿问

萌新!几个数组合并然后根据数组对象中的数组去重求老司机指点!

假设有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是否相等,有什么优雅的解决方案,目前嵌套了好几层,实现是可以实现,但是感觉不优雅,想求个优雅的方案洗洗脑。


HUWWW
浏览 270回答 2
2回答

拉莫斯之舞

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))试试这样?
随时随地看视频慕课网APP

相关分类

Java
我要回答