2

CHANGELOG

2019-07-01

  • init

const permutation = (source) => {
  const result = [];
  const _result = {};
  const convert = (arr, index) => {
    for (let i = 0; i < source[arr[index]].length; i++) {
      if (source[arr[index]][i]) {
        _result[arr[index]] = source[arr[index]][i]
        if (index === arr.length - 1) {
          result.push(JSON.parse(JSON.stringify(_result)));
        } else {
          convert(arr, index + 1);
        }
      }
    }
  };
  convert(Object.keys(source), 0);
  return result;
};

// 测试
permutation({
  name: ['张三','李四','王二'],
  age: [18, 16],
  sex: ['男', '女']
});
测试结果如下
图片描述

vdfor
100 声望9 粉丝

大前端