0

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?

5
  • Add a desired output and what you have tried so far please. Commented Aug 28, 2019 at 9:39
  • Can you share the desired output as well? Do you want text2 fields to be converted to text1? Commented Aug 28, 2019 at 9:39
  • i need this result 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, }] } Commented Aug 28, 2019 at 9:47
  • i try with push let merge= [] for(let i=0; i<arr2.length; i++) { merged.push({ ...arr2[i], ...arr1[i] }); } Commented Aug 28, 2019 at 9:48
  • @Ismeet check my answer you will get that Commented Aug 28, 2019 at 9:53

2 Answers 2

1

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}]}]
Sign up to request clarification or add additional context in comments.

1 Comment

i need to merge in one array and concat text2 to firt 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, }] } –
0

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()))

3 Comments

it's because I made the reduce return an object. Check the an answer again you will see that I updated it to return an array.
looks like i didn't get the desired result. I only have one in each item. I update my desired output.
this is desire uotput 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, } ] } ]

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.