2

I have two arrays of objects. How can I combine/concat them in to one array of object.

I have tried using the concat function as well iterating through arr2 and pushing them in arr1, but I want to do in a shorter way instead.

let arr1 = [{ _id: 1, external_id: '74341f74-9c79-49d5-9611-87ef9b6eb75f', name: 'Francisca Rasmussen', alias: 'Miss Coffey' }, { _id: 19, external_id: '68e35e26-7b1f-46ec-a9e5-3edcbcf2aeb9', name: 'Francis Rodrigüez', alias: 'Mr Lea' }, { _id: 23, external_id: 'e9db9277-af4a-4ca6-99e0-291c8a97623e', name: 'Francis Bailey', alias: 'Miss Singleton' }]; let arr2 = [ { organizations: 'Multron', joining_key: 1 }, { organizations: 'Bitrex', joining_key: 19 }, { organizations: 'Enthaze', joining_key: 23 }, { tickets: 'A Nuisance in Kiribati', joining_key: 1 }, { tickets: 'A Nuisance in Saint Lucia', joining_key: 19 } { tickets: 'A Nuisance in Saint Kilda', joining_key: 19 } ] 

I want to concat them which gives result as below based on the joining_key which is _id field on arr1:

 [{ _id: 1, external_id: '74341f74-9c79-49d5-9611-87ef9b6eb75f', name: 'Francisca Rasmussen', alias: 'Miss Coffey', organizations: 'Multron', tickets: 'A Nuisance in Kiribati', joining_key: 1 }, { _id: 19, external_id: '68e35e26-7b1f-46ec-a9e5-3edcbcf2aeb9', name: 'Francis Rodrigüez', alias: 'Mr Lea', organizations: 'Bitrex', tickets: 'A Nuisance in Saint Lucia', tickets: 'A Nuisance in Saint Kilda' }, { _id: 23, external_id: 'e9db9277-af4a-4ca6-99e0-291c8a97623e', name: 'Francis Bailey', alias: 'Miss Singleton', organizations: 'Enthaze' }] 
9
  • 1
    What happens if the arrays contain additional objects? Commented Apr 5, 2019 at 23:36
  • 1
    It's very unclear why arr1 is an array and not a single object and what should happen if the array has another object. Commented Apr 5, 2019 at 23:37
  • @Amy If arr2 contains any additional objects they are all combined to arr1. Eg : If arr2 contains tickets_3, the result will have ticket_3 as well in the resulting object Commented Apr 5, 2019 at 23:38
  • 1
    @MarkMeyer Ah, i see that now. I was mentally combining them. Commented Apr 5, 2019 at 23:40
  • 1
    @shadoe2020 The OP is not trying to concatenate two arrays. Your answer was incorrect. Commented Apr 5, 2019 at 23:42

5 Answers 5

1

You can use Object.assign(...arr1) for this case

More reference for Spread Operator at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

let arr1 = [{ _id: 1, external_id: '74341f74-9c79-49d5-9611-87ef9b6eb75f', name: 'Francisca Rasmussen', alias: 'Miss Coffey', created_at: '2016-04-15T05:19:46 -10:00' }]; let arr2 = [ { organizations: 'Multron' }, { tickets_1: 'A Nuisance in Kiribati' }, { tickets_2: 'A Nuisance in Saint Lucia' } ]; arr1 = arr1.concat(arr2) //console.log(arr1) let result = [Object.assign(...arr1)] console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

1

With the edits, it's a much different question. Your array has _id properties which you want to use to lookup the items. arr1 should probably be an object, which would make it possible to find a particular item given an id without searching the whole list . In fact, it's probably best to make a lookup object you can use to merge the objects (although you could use find() on the array.

Then just step through the arr2 and add the objects to the correct item from arr1:

let arr1 = [{_id: 1,external_id: '74341f74-9c79-49d5-9611-87ef9b6eb75f',name: 'Francisca Rasmussen',alias: 'Miss Coffey'},{ _id: 19,external_id: '68e35e26-7b1f-46ec-a9e5-3edcbcf2aeb9',name: 'Francis Rodrigüez',alias: 'Mr Lea'},{ _id: 23,external_id: 'e9db9277-af4a-4ca6-99e0-291c8a97623e',name: 'Francis Bailey',alias: 'Miss Singleton'}]; let arr2 = [ { organizations: 'Multron', joining_key: 1 },{ organizations: 'Bitrex', joining_key: 19 },{ organizations: 'Enthaze', joining_key: 23 },{ tickets: 'A Nuisance in Kiribati', joining_key: 1 },{ tickets: 'A Nuisance in Saint Lucia', joining_key: 19 },{ tickets: 'A Nuisance in Saint Kilda', joining_key: 19 }] // make lookup let lookup = arr1.reduce((obj, item) => (obj[item._id] = item, obj), {}) // merge objects using lookup arr2.forEach(item => { let {joining_key, ...rest} = item // seperate join_key-it's not in the final result Object.assign(lookup[joining_key], rest) }) // arr1 now has merged objects console.log(console.log(arr1))

Note: your desired result gives the object with _id: 19 two tickets properties. Properties must be unique on objects, so this isn't possible.

Comments

0

Push the arrays into another array Docs

//array1 with some content let array1=["item1","item2"]; //array2 with some content let array2=["item3","item4"]; //create empty array let array3=[]; //concatenate arrays //add array1 array3.push(array1); //add array2 array3.push(array2); //display result console.log(array3); 

Comments

0

Use the spread operator

const arr3 = [{ ...arr1[0], ...arr2[0] }] 

Quite straightforward, isn't it?

In your case, this would return an array with a single object (the two objects are now combined)

Comments

0

I think this one will give you answers i don't know if its best solution but it worked on me.

arr1.forEach(element => { var filtered = arr2.filter(obj => obj.joining_key === element._id); filtered.forEach(filtered_object => { let i = 0; Object.keys(filtered_object).forEach(key => { if (i == 0) { element[key] = filtered_object[key] } i = i + 1; }) }) }) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.