Ismeet
Ismeet

Reputation: 449

Merge two array javascript

I have two almost identical arrays of objects, the only difference between them is that the first array consists of inner arrays with objects having a text1 attribute, and the second array has text2 property instead. Each array contains 800 objects with same keys 1, 2, 3, 4 ....

How can I merge these two arrays? I tried with push function but got only the first array. Is there some other way to merge two arrays like reduce, filter or some other function?

let arr1 = [{
    "1": [{
        "text1": "Some text 1",
        "englishName": "Name 1",
        "number": 1,
      },
      {
        "text1": "Some text 2",
        "englishName": "Name 2",
        "number": 2,
      }
    ]
  },
  {
    "2": [{
        "text1": "Some text 3",
        "englishName": "Name 3",
        "number": 3,
      },
      {
        "text1": "Some text 4",
        "englishName": "Name 4",
        "number": 4,
      }
    ]
  }
]

let arr2 = [{
    "1": [{
        "text2": "Some new text",
        "englishName": "Name 1",
        "number": 1
      },
      {
        "text2": "Some new text 2",
        "englishName": "Name 2",
        "number": 2,
      }
    ]
  },
  {
    "2": [{
        "text2": "Some new text 3",
        "englishName": "Name 3",
        "number": 3,
      },
      {
        "text2": "Some new text 4",
        "englishName": "Name 4",
        "number": 4,
      }
    ]
  }
]

i need to merge in one array and concat text2 to first array like this

  let mergearray = [
  {
  "1": [
   {
    "text1": "Some text 1",
    "text2": "Some new text 1",
    "englishName": "Name 1",
    "number": 1,
   },
   {
    "text1": "Some text 2",
    "text2": "Some new text 2",
    "englishName": "Name 2",
    "number": 2,
  }
  ]
  },
 {
 "2": [
   {
    "text1": "Some text 3",
    "text2": "Some new text 3",
    "englishName": "Name 3",
    "number": 3,
   },
   {
    "text1": "Some text 4",
    "text2": "Some new text 4",
    "englishName": "Name 4",
    "number": 4,
   } ] } ]

How compare keys of arrays?

Upvotes: 0

Views: 6547

Answers (2)

Akram Badah
Akram Badah

Reputation: 427

Try this:

const arr3 = arr1.reduce((acc, item, index, array) => {
  if (arr2.length > index) {
    array[index][index + 1][0].text2 = arr2[index][index + 1][0].text2;
    acc = array.map((item, index2) => {
      return item[index2 + 1][0];
    });
  }
  return acc;
}, []);
console.log(JSON.stringify(arr3.reverse()))

Upvotes: 0

ArunPratap
ArunPratap

Reputation: 5030

you can use JavaScript Array concat() method the concat() method is used to join two or more arrays. This method does not change the existing arrays, it returns a new array, containing the values of the joined arrays. you can do that like this

let arr= arr1.concat(arr2); 
console.log(JSON.stringify(arr));

it will give you this result

[{"1":[{"text1":"Some text 1","englishName":"Name 1","number":1},{"text1":"Some text 2","englishName":"Name 2","number":2}]},
{"2":[{"text1":"Some text 3","englishName":"Name 3","number":3},{"text1":"Some text 4","englishName":"Name 4","number":4}]},
{"1":[{"text2":"Some new text","englishName":"Name 1","number":1},{"text2":"Some new text 2","englishName":"Name 2","number":2}]},
{"2":[{"text2":"Some new text 3","englishName":"Name 3","number":3},{"text2":"Some new text 4","englishName":"Name 4","number":4}]}]

Upvotes: 1

Related Questions