3

I have several objects that look like this when i console.log them:

0: { 'school': {}, 'district': {}, 'children': {} } 1: { 'school': {}, 'district': {}, 'children': {} } 2: { 'school': {}, 'district': {}, 'children': {} } 

and

0: { 'teacher': {}, 'name': {}, 'class': {} } 1: { 'teacher': {}, 'name': {}, 'class': {} } 

I am looking to combine these objects so that they become like:

0: { 'school': {}, 'district': {}, 'children': {} } 1: { 'school': {}, 'district': {}, 'children': {} } 2: { 'school': {}, 'district': {}, 'children': {} } 3: { 'teacher': {}, 'name': {}, 'class': {} } 4: { 'teacher': {}, 'name': {}, 'class': {} } 

But when I use something like Object.assign the last object will just overwrite the previous one, is there another method to do this? I basically just want to merge several objects, and ignore their indexes.

3
  • they look like arrays, not objects. Array.concat would be used for that Commented Apr 20, 2018 at 18:08
  • 1
    If you don't care about the keys, just make them an array: const foo = [...Object.values(obj1), ...Object.values(obj2)] Commented Apr 20, 2018 at 18:09
  • push them all in one array Commented Apr 20, 2018 at 18:09

4 Answers 4

1

It seems like what you have is an array.

You can use the spread operator.

Let's call your two arrays arrayOne and arrayTwo respectively.

The code would be:

const newArray = [...arrayOne, ...arrayTwo];

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

1 Comment

One quible, OP gave objects, you need to call Object.values like in my comment above.
1

you can use spread operator

const arr1 = [	{ 'school': {}, 'district': {}, 'children': {} },	{ 'school': {}, 'district': {}, 'children': {} },	{ 'school': {}, 'district': {}, 'children': {} } ] const arr2 = [	{ 'teacher': {}, 'name': {}, 'class': {} },	{ 'teacher': {}, 'name': {}, 'class': {} } ] const result = [...arr1, ...arr2] console.log(result)

3 Comments

@nxmohamad beat you to the punch.
@JaredSmith 50 seconds late :P , but Object.values is more accurate anyways
lol technically I beat him by 15 seconds, but I just left it in a comment.
0

var array1 = [{ 'school': {}, 'district': {}, 'children': {} }, { 'school': {}, 'district': {}, 'children': {} }, { 'school': {}, 'district': {}, 'children': {} }]; var array2 = [ { 'teacher': {}, 'name': {}, 'class': {} }, { 'teacher': {}, 'name': {}, 'class': {} }]; while(array2.length>0){ array1.push(array2.shift()); } console.log(array1);

Comments

0

You could concat the values of the objects and assign the array to an object.

var obj1 = { 0: { 'school': {}, 'district': {}, 'children': {} }, 1: { 'school': {}, 'district': {}, 'children': {} }, 2: { 'school': {}, 'district': {}, 'children': {} } }, obj2 = { 0: { 'teacher': {}, 'name': {}, 'class': {} }, 1: { 'teacher': {}, 'name': {}, 'class': {} } }; obj3 = Object.assign({}, Object.values(obj1).concat(Object.values(obj2))); console.log(obj3);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.