Union of Array of Objects in JavaScript?

前端 未结 6 940
予麋鹿
予麋鹿 2020-12-03 12:11

So after searching the interwebz for a few hours I have not found the solution I am looking for.

I have two arrays that contain game objects with a lot of informatio

6条回答
  •  鱼传尺愫
    2020-12-03 12:48

    Set (ES6/ES2015) will help you.

    const info1 = {id: 1}
    const info2 = {id: 2}
    const info3 = {id: 3}
    
    const array1 = [info1, info2]
    const array2 = [info1, info3]
    
    const union = [...new Set([...array1, ...array2])]
    
    console.log(union)

提交回复
热议问题