最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to merge two arrays which are inside individual objects as property - Stack Overflow

programmeradmin8浏览0评论

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.

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.

Share Improve this question asked Dec 25, 2019 at 8:01 Mian MuhammadMian Muhammad 5162 gold badges7 silver badges24 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 16

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)

With Vanilla JS you can iterate with Array.flatMap() and return the slotIDs to get an array of ids. To remove duplicates, create a Set from the array, and spread the Set back to an array:

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

const result = [...new Set([obj1, obj2].flatMap(o => o.slotIDs))]

console.log(result)

With lodash you can iterate with _.flatMap() and take the slotIDs to get an array of ids. Use _.uniq() To remove duplicates:

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

const result = _.uniq(_.flatMap([obj1, obj2], 'slotIDs'))

console.log(result)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.15/lodash.js"></script>

Try

let newObj= []
for(const value in Object.assign(obj1,obj2)["slotIDs"])
newObj.push(Object.assign(obj1,obj2)["slotIDs"][value])

Edit- Here's a simpler or one-line version.

let newObj=Object.assign(obj1,obj2)["slotIDs"]

As @OriDrori Suggested, the above methods alters the obj1 itself and doesn't works well in similar questions where Obj1 has multiple key, value pair. Here's what you do to avoid that

let newObj=Array.from(new Set(obj1.slotIDs.concat(obj2.slotIDs)))

Quick Note- Use of Array.from() is optional.

Object.assign(obj1, obj2).slotIDs

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

const result = Object.assign(obj1, obj2).slotIDs

console.log(result)

EDIT:

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

let result = Array.from( new Set(obj1.slotIDs.concat(obj2.slotIDs)) )

console.log(result)


Old answer:

How about { ...obj1, ...obj2 }.slotIDs?

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

let result = { ...obj1, ...obj2 }.slotIDs

console.log(result)

If your object can have additional properties which hold array values and you would like to merge all of these into a unique array you can use Object.entries() with .map() and then a Set to remove the duplicates:

const obj1 = { slotIDs: ["5e0301f353ee2a0546298f15"], itemIds: ["xyz123"] };
const obj2 = { slotIDs: ["5e0301f353ee2a0546298f15", "5e03050453ee2a0546298f1c"], itemids: ["xyz123", "abc123"] };

const res = [...new Set([obj1, obj2].map(Object.values).flat(2))];
console.log(res);

发布评论

评论列表(0)

  1. 暂无评论