简体   繁体   English

合并两个数组javascript

[英]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.我有两个几乎相同的对象数组,它们之间的唯一区别是第一个数组由具有text1属性的对象的内部数组组成,而第二个数组具有text2属性。 Each array contains 800 objects with same keys 1 , 2 , 3 , 4 ....每个数组包含 800 个对象,具有相同的键1234 ....

How can I merge these two arrays?如何合并这两个数组? I tried with push function but got only the first array.我尝试使用push函数,但只得到了第一个数组。 Is there some other way to merge two arrays like reduce , filter or some other function?是否有其他方法可以合并两个数组,例如reducefilter或其他一些函数?

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我需要合并一个数组并将 text2 连接到这样的第一个数组

  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?如何比较数组的键?

you can use JavaScript Array concat() method the concat() method is used to join two or more arrays .您可以使用JavaScript Array concat()方法concat()方法用于连接两个或多个数组 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}]}]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM