Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

数组对像合并,用新的内容覆盖老的怎么解决

数组对像合并

//默认数组队象

list:[
    {a:'标题',b:'5',c:'text'},
    {a:'简介',b:'50',c:'text'},
]

新设置的数组队象 覆盖合并到上面的数组队象中,用新的数据覆盖默认数据,

list:[
    {a:'标题',b:'10',d:'time'},
    {a:'简介',b:'150'},
]

最终合并后得到

list:[
    {a:'标题',b:'10',c:'text',d:'time'},
    {a:'简介',b:'150',c:'text'},
]

求问有什么好方法!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

如果a是唯一值

let list1 = [
    {a:'标题',b:'5',c:'text'},
    {a:'简介',b:'50',c:'text'},
]
const list2 = [
    {a:'标题',b:'10',d:'time'},
    {a:'简介',b:'150'},
]

list1 = list1.map(item => {const temp = list2.find(r => r.a===item.a)||{};return {...item,...temp} })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...