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

Derek Wang
  • 10,098
  • 4
  • 18
  • 39
CoCo
  • 93
  • 1
  • 9
  • 1
    Does this answer your question? [how can I concatenate two arrays in javascript?](https://stackoverflow.com/questions/36989741/how-can-i-concatenate-two-arrays-in-javascript) – Heretic Monkey Oct 09 '20 at 14:07

1 Answers1

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);
Derek Wang
  • 10,098
  • 4
  • 18
  • 39