0

这是我在下面编写的代码中的两个数组。我写了一个函数来添加他们的元素,但我想做一些可以接受任意数量的数组并相应地加入它们的东西!

数组 1:

var product =
[ [ 'Product Code' ],
[ 'AFC' ],
[ 'MFC' ],
[ 'AC' ],
[ 'MC' ],
[ 'AT' ],
[ 'MT' ],
[ 'AD' ],
[ 'MTB' ],
[ 'HBB' ] ]

数组 2:

var quantity = 
[ [ 'S', 'M', 'L', 'XL' ],
[ 16, 17, 6, 0 ],
[ 4, 2, 0, 0 ],
[ 2, 11, 0, 0 ],
[ 2, 6, 2, 0 ],
[ 6, 17, 1, 0 ],
[ 0, 2, 0, 0 ],
[ 0, 1, 0, 0 ],
[ 0, 1, 0, 0 ],
[ 0, 0, 1, 0 ] ]

var final = msJoin(product,qty);

final = 
[ [ 'Product Code', 'S', 'M', 'L', 'XL' ],
  [ 'AFC', 16, 17, 6, 0 ],
  [ 'MFC', 4, 2, 0, 0 ],
  [ 'AC', 2, 11, 0, 0 ],
  [ 'MC', 2, 6, 2, 0 ],
  [ 'AT', 6, 17, 1, 0 ],
  [ 'MT', 0, 2, 0, 0 ],
  [ 'AD', 0, 1, 0, 0 ],
  [ 'MTB', 0, 1, 0, 0 ],
  [ 'HBB', 0, 0, 1, 0 ] ]

使用以下代码:

function msJoin(array1,array2){

  var concatArray = (array1,array2)=>{
    
    var final = [];

    for(var i = 0 ; i<array1.length;i++){
      
      var tempElement = [];

      array1[i].forEach(
      
        function (ee){
          tempElement.push(ee);
          console.log(ee);
        }
      );
      array2[i].forEach(
      
        function (ee){
          tempElement.push(ee);
          console.log(ee);
        }
      );
      
      final.push(tempElement);
    }

    return final;
  }

  return concatArray(array1,array2);
}

如何为动态数量的数组编写它?

ms(array1,array2,array3.......);

4

4 回答 4

2

您可以使用 forEach 从任意数量的数组中添加单个数组中的元素。

你也可以使用

  result[j] = [...result[j], ...val];

var product = [
  ["Product Code"],
  ["AFC"],
  ["MFC"],
  ["AC"],
  ["MC"],
  ["AT"],
  ["MT"],
  ["AD"],
  ["MTB"],
  ["HBB"],
];

var second = [
  ["temp"],
  ["temp"],
  ["temp"],
  ["temp"],
  ["temp"],
  ["temp"],
  ["temp"],
  ["temp"],
  ["temp"],
  ["temp"],
];

var quantity = [
  ["S", "M", "L", "XL"],
  [16, 17, 6, 0],
  [4, 2, 0, 0],
  [2, 11, 0, 0],
  [2, 6, 2, 0],
  [6, 17, 1, 0],
  [0, 2, 0, 0],
  [0, 1, 0, 0],
  [0, 1, 0, 0],
  [0, 0, 1, 0],
];

function join(...arrs) {
  const result = Array(arrs[0].length).fill([]);

  arrs.forEach((arr, i) => {
    arr.forEach((val, j) => {
      result[j] = result[j].concat(val);
    });
  });

  return result;
}
console.log(join(product, second, quantity));

于 2021-06-23T12:08:50.820 回答
2

您可以简单地使用reduceandmap来获得您想要的结果。

function joinArray(...arr) {
  return arr.reduce((a, b) => a.map((e, i) => [...e, ...b[i]]));
}

var product = [
  ["Product Code"],
  ["AFC"],
  ["MFC"],
  ["AC"],
  ["MC"],
  ["AT"],
  ["MT"],
  ["AD"],
  ["MTB"],
  ["HBB"],
];

var quantity = [
  ["S", "M", "L", "XL"],
  [16, 17, 6, 0],
  [4, 2, 0, 0],
  [2, 11, 0, 0],
  [2, 6, 2, 0],
  [6, 17, 1, 0],
  [0, 2, 0, 0],
  [0, 1, 0, 0],
  [0, 1, 0, 0],
  [0, 0, 1, 0],
];

var brand = [
  ["Sasdf"],
  ["asdg"],
  ["223"],
  ["asdf"],
  ["gasdf"],
  ["asdfa"],
  ["232"],
  ["gasdf"],
  ["asdf"],
  ["323"],
];

const output = joinArray(product, quantity, brand);

console.log(output);

于 2021-06-23T12:55:27.740 回答
1

function add() {
  let a = 0
  Object.keys(arguments).forEach(ckey => {
    a = a + arguments[ckey]
  })

  console.log(a)
}

add(1)

add(1, 4, 21, 123, 1)

于 2021-06-23T11:54:36.093 回答
1

如果你导入了 lodash,

import {zip, join} from 'lodash';

const product =
[ [ 'Product Code' ],
[ 'AFC' ],
[ 'MFC' ],
[ 'AC' ],
[ 'MC' ],
[ 'AT' ],
[ 'MT' ],
[ 'AD' ],
[ 'MTB' ],
[ 'HBB' ] ];

const quantity = 
[ [ 'S', 'M', 'L', 'XL' ],
[ 16, 17, 6, 0 ],
[ 4, 2, 0, 0 ],
[ 2, 11, 0, 0 ],
[ 2, 6, 2, 0 ],
[ 6, 17, 1, 0 ],
[ 0, 2, 0, 0 ],
[ 0, 1, 0, 0 ],
[ 0, 1, 0, 0 ],
[ 0, 0, 1, 0 ] ];

const result = zip(product, quantity).map(e => [join(e)]);
console.log(result);
于 2021-06-23T11:59:53.153 回答