来自第一个数组的所有可能值的组合?

时间:2019-03-22 14:46:26

标签: javascript

如何基于第一个array1或基本按array1组合数组的数组。

下面是四个数组,我必须在其中形成基于A的对象,然后基于B的对象。

var array1=["A","B"];
var array2=["1","2","3", "4"];
var array3=["N","O","P", "Q"];
var array4=["R"];

下面是我的需要:

[ {
  'take': 'A', 
  'take2': '1',
  'take3': 'N',
  'take4': 'R'
}, {
  'take': 'A', 
  'take2': '2',
  'take3': 'N',
  'take4': 'R'
}, {
  'take': 'A', 
  'take2': '3',
  'take3': 'N',
  'take4': 'R'
}, {
  'take': 'A', 
  'take2': '4',
  'take3': 'N',
  'take4': 'R'
}, {
  'take': 'A', 
  'take2': '1',
  'take3': 'O',
  'take4': 'R'
}]

这是我尝试过的方法,但不确定如何循环n个n个数组

var result = array1.reduce( (a, v) =>
   [...a, ...array2.map(x=>v+x)],
[]);

3 个答案:

答案 0 :(得分:3)

这是一个简单的解决方案(如果您知道有多少个数组):

const possibilities = [];

const ar1length = array1.length;
const ar2length = array2.length;
const ar3length = array3.length;
const ar4length = array4.length;

// Not cleanest solution available but it does the job

for ( let i = 0; i < ar1length; i++) {

    for (let j = 0; j < ar2length; j++) {

        for (let k = 0; k < ar3length; k++) {

            for (let l = 0; l < ar4length; l++) {

                possibilities.push({
                    "take": array1[i],
                    "take1": array2[j],
                    "take2": array3[k],
                    "take3": array4[l]
                });


            }


        }

    }
}

哦,如果您想要未知数量的数组,可以将所有这些数组添加到数组中,以便对其进行迭代

答案 1 :(得分:1)

前段时间我已经为此任务编写了一个函数,它接受任意数量的数组和非数组并计算所有可能的组合

var array1 = ["A", "B"];
var array2 = ["1", "2", "3", "4"];
var array3 = ["N", "O", "P", "Q"];
var array4 = ["R"];

console.log(combinations(array1, array2, array3, array4).join("\n"));

function combinations(...columns) {
  const state = [], combinations = [state];
  let head = null;
  for (let column = 0; column < columns.length; ++column) {
    let value = columns[column];
    if (Array.isArray(value)) {
      if (value.length > 1) {
        head = {
          next: head,
          column,
          row: 0
        };
      }
      value = value[0];
    }
    state[column] = value;
  }

  let todo = head;
  while(todo) {
    if (++todo.row === columns[todo.column].length) {
      todo.row = 0;
      state[todo.column] = columns[todo.column][todo.row];
      todo = todo.next;
    } else {
      state[todo.column] = columns[todo.column][todo.row];
      combinations.push(state.slice());
      todo = head;
    }
  }

  return combinations;
}
.as-console-wrapper{top:0;max-height:100%!important}

答案 2 :(得分:1)

这是一种递归方法,适用于每一个数组,只需调用combine(array1, array2, ..., arrayn)

var array1=["A","B"];
var array2=["1","2","3", "4"];
var array3=["N","O","P", "Q"];
var array4=["R"];

function combine(arr1, ...arr2) {
  if(arr2.length === 0) return Array.from(arr1, (x) => x.reduce((obj, y, i) => (obj[`take${i}`] = y, obj), {}));
  
  return combine(arr1.flatMap(d => arr2[0].map(v => {
  return [...Object.values(d), ...Object.values(v)]
  })), ...arr2.slice(1));
}

console.log(combine(array1, array2, array3, array4));