0

I have following two objects

First:

var a = [{a:1},
 {b:2},
 {c:3}
]

Second:

var b = [{a:1},
 {b:2},
 {c:3}
]

I am trying to merge the objects using following function which I wrote:

var json_concat = async (o1, o2) => {
    return new Promise((resolve, reject) => {
        try {
            for (var key in o2) {
                o1[key] = o2[key];
            }
            resolve(o1);
        } catch (e) {
            reject(e);
        }
    });
}

I am not getting the concatenated object by this. The required result is:

var c = [
{a:1},
{b:2},
{c:3},
{a:1},
{b:2},
{c:3}
]

Please help. Thanks in advance

1

1 Answer 1

2

You don't need to make a specific concat function to get that result. Using Array.concat, you can get the result easily.

var a = [
  {a:1},
  {b:2},
  {c:3}
];

var b = [
  {a:1},
  {b:2},
  {c:3}
];

const result = a.concat(b);
console.log(result);

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

1 Comment

This is a common question and at your rep you should be familiar with what kinds of questions will have been asked before.

Your Answer

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.