0

I need to merge two arrays with objects inside with JavaScript.

How can this be done?

I am currently using this code, but it is not for me:

var array_merge = [];
var array_one = [
  {
    label: "label",
    value: "value",
  },
  {
    label: "label",
     value: "value",
  },
];
array_merge.push(array_one);
var array_two = [
  {
    label: "label",
    value: "value",
  },
  {
    label: "label",
     value: "value",
  },
];
array_merge.push(array_two);

How can I join them so that the result is the following?

var array_merge = [
  {
    label: "label",
    value: "value",
  },
  {
    label: "label",
    value: "value",
  },
  {
    label: "label",
    value: "value",
  },
  {
    label: "label",
    value: "value",
  },
];

Thanks.

Matteo Feduzi
  • 55
  • 1
  • 8

1 Answers1

0

You can use the concat method to merge arrays

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

const array_one = [
  {
    label: "label",
    value: "value",
  },
  {
    label: "label",
     value: "value",
  },
];
const array_two = [
  {
    label: "label",
    value: "value",
  },
  {
    label: "label",
     value: "value",
  },
];

const array_merge = array_one.concat(array_two);

console.log(array_merge)
RenaudC5
  • 3,553
  • 1
  • 11
  • 29
  • At the end I've solved with the code in this ticket: https://stackoverflow.com/questions/7146217/merge-2-arrays-of-objects Thanks anyway! – Matteo Feduzi May 12 '22 at 09:22