Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two arrays which are inside individual objects as property

I have two objects like this:

let obj1 = { slotIDs: ["5e0301f353ee2a0546298f15"] }
let obj2 = { slotIDs: ["5e0301f353ee2a0546298f15", "5e03050453ee2a0546298f1c"] }

I need to merge them inside a single array like this

let newObj = ["5e0301f353ee2a0546298f15", "5e03050453ee2a0546298f1c"]

I have tried using lodash union and map but no luck.

like image 418
Mian Muhammad Avatar asked Dec 25 '19 08:12

Mian Muhammad


1 Answers

One line of code solution:

let obj1 = { slotIDs: ['5e0301f353ee2a0546298f15'] }
let obj2 = { slotIDs: ['5e0301f353ee2a0546298f15', '5e03050453ee2a0546298f1c'] }

const result = [...new Set([...obj1.slotIDs, ...obj2.slotIDs])]
console.log(result)
like image 162
Loi Nguyen Huynh Avatar answered Oct 22 '22 07:10

Loi Nguyen Huynh