1

I am populating a json file and I have an array like this

[{ name: name, address: address, age: age }]
[{ name2: name2, address2: address2, age2: age2 }]

Now, I want to merge these two arrays and I'm expecting a result like this

[
 { name: name, address: address, age: age },
 { name2: name2, address2: address2, age2: age2 }
]
crashmstr
28.7k9 gold badges66 silver badges80 bronze badges
asked May 30, 2019 at 11:49
4
  • 1
    So what's the question? What's the problem? What have you tried so far? Commented May 30, 2019 at 12:22
  • 1
    const array3 = [...array1, ...array2];? Commented May 30, 2019 at 12:55
  • Please see the link below stackoverflow.com/questions/7146217/… Commented May 30, 2019 at 13:04
  • Please see the link below stackoverflow.com/questions/7146217/merge-2-arrays-of-objects Commented May 30, 2019 at 13:09

2 Answers 2

2

Another option...

var array1 = [{ name: name, address: address, age: age }]
var array2 = [{ name2: name2, address2: address2, age2: age2 }]
var merged = array1.concat(array2)
answered May 30, 2019 at 12:58
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting a result like this [{array1},{array2},{array3},{array4}] and i am expecting result like this [{array1{array3}},{array2{array4}}]
Your expected result contradicts your original question. You should post the code that you have tried in your question so people can provide a more satisfying answer.
1

 const a = [{ name: 'name1', address: 'address1', age: 22 }];
 
 const b = [{ name2: 'name2', address2: 'address2', age2: 23 }];
 
 const result = [...a, ...b];
 
 console.log(result);

answered May 30, 2019 at 12:57

1 Comment

I am getting a result like this [{array1},{array2},{array3},{array4}] and i am expecting result like this [{array1{array3}},{array2{array4}}]

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.