简体   繁体   English

加盟 arrays Javascript

[英]Joining arrays Javascript

I have two arrays:我有两个 arrays:

Array1 = [
  [{
    index: '1',
    value: '100'
  }, {
    index: '2',
    value: '200'
  }],
  [{
    index: '1.1',
    value: '100'
  }, {
    index: '1.2',
    value: '200'
  }]
];

Array2 = [
  [{
    index: '10',
    value: '100'
  }, {
    index: '20',
    value: '200'
  }],
  [{
    index: '10.1',
    value: '100'
  }, {
    index: '10.2',
    value: '200'
  }]
]

How can I join the two arrays so that, the resulting array would be我怎样才能加入两个 arrays 这样,结果数组就是

ResultArray = [
  [{
    index: '1',
    value: '100'
  }, {
    index: '2',
    value: '200'
  }, {
    index: '10',
    value: '100'
  }, {
    index: '20',
    value: '200'
  }],
  [
    [{
      index: '1.1',
      value: '100'
    }, {
      index: '1.2',
      value: '200'
    }], {
      index: '10.1',
      value: '100'
    }, {
      index: '10.2',
      value: '200'
    }
  ]
]

Thanks in advance提前致谢

As comments said, you might need to separate them by whether index is integer string or float string.正如评论所说,您可能需要通过索引是 integer 字符串还是浮点字符串来分隔它们。

Here's an example of how to do separation.这是一个如何进行分离的示例。

 Array1 = [ [{ index: '1', value: '100' }, { index: '2', value: '200' }], [{ index: '1.1', value: '100' }, { index: '1.2', value: '200' }] ]; Array2 = [ [{ index: '10', value: '100' }, { index: '20', value: '200' }], [{ index: '10.1', value: '100' }, { index: '10.2', value: '200' }] ] ResultArray = [[],[]]; // first combine all of the elements. var arr = []; [...Array1, ...Array2].forEach(function(el){ arr.push(...el); }) // check all elements and set to result array arr.forEach(function(el){ // check whether it is integer or not to set to index 0 or 1 // if Number(el.index) is integer, mod 1 will equal to 0, else it is float. var val = Number(el.index); if(val % 1 === 0) ResultArray[0].push(el); else ResultArray[1].push(el); }) console.log(ResultArray);

This could be made more efficient, but for simplicity it has been separated into two steps.这可以变得更有效率,但为简单起见,它已分为两个步骤。 The first step is to flatten all the objects into one array.第一步是将所有对象展平到一个数组中。 The second is to bin their contents into an array based on their index value (integer vs float):第二种是根据它们的索引值(整数与浮点数)将它们的内容放入一个数组中:

 let Array1 = [ [{ index: '1', value: '100' }, { index: '2', value: '200' }], [{ index: '1.1', value: '100' }, { index: '1.2', value: '200' }] ]; let Array2 = [ [{ index: '10', value: '100' }, { index: '20', value: '200' }], [{ index: '10.1', value: '100' }, { index: '10.2', value: '200' }] ] let ResultArray = [...flatten(Array1), ...flatten(Array2)] console.log('Step 1: Flatten\n', ResultArray) ResultArray = ResultArray.reduce((acc, o) => { let val = +o['index']; (val % 1 === 0)? acc[0].push(o): acc[1].push(o) return acc; }, [[],[]]); console.log('Step 2: Bin\n', ResultArray) function flatten(arr) { return arr.reduce((acc, val) => Array.isArray(val)? acc.concat(flatten(val)): acc.concat(val), []); }

Simply concat the source arrays, and loop over them.简单地连接源代码 arrays,然后遍历它们。 In each iteration, check if the index is a float or integer, and based on the decision, push the results to separate arrays.在每次迭代中,检查index是否为浮点数或 integer,并根据决定将结果推送到单独的 arrays。

 var arr1= [[{index:'1',value:'100'},{index:'2',value:'200'}],[{index:'1.1',value:'100'},{index:'1.2',value:'200'}]]; var arr2 = [[{index:'10',value:'100'},{index:'20',value:'200'}],[{index:'10.1',value:'100'},{index:'10.2',value:'200'}]]; var ints = [], floats = []; function classify(items) { items.forEach(item => (/^\d+$/.test(item.index)? ints: floats).push(item)); } arr1.concat(arr2).forEach(classify); console.log([ints, floats]);

Hope this helps.希望这可以帮助。 I executed this on atom as a javascript printing the list in the console log.我在 atom 上执行此操作作为 javascript 在控制台日志中打印列表。

 var Array1 = [ [{ index:'1', value:'100' }, { index:'2', value:'200' }], [{ index:'1.1', value:'100' }, { index:'1.2', value:'200' }] ]; var Array2 = [ [{ index:'10', value:'100' }, { index:'20', value:'200' }], [{ index:'10.1', value:'100' }, { index:'10.2', value:'200' }] ]; Array1.push(Array2) console.log(Array1)

This code only works if Array1 and Array2 have the same size.此代码仅在 Array1 和 Array2 具有相同大小时才有效。 This code combines the objects in Array1 and Array2 according to the respective keys.此代码根据各自的键组合 Array1 和 Array2 中的对象。

var arr = [];
for (var i = 0; i < Array1.length; i++){
    arr.push(Array1[i].concat(Array2[i]));
}
console.log(arr);

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

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