【问题标题】:JavaScript - Generating combinations from dictionary keys and keeping key names dynamicallyJavaScript - 从字典键生成组合并动态保持键名
【发布时间】:2018-06-20 03:36:19
【问题描述】:

我在这里找到了这个生成多个arrays 的所有组合的优秀代码:JavaScript - Generating combinations from n arrays with m elements

我现在希望更进一步,我想生成包含 arrays 的多个 JSON 对象的所有组合

例如,如果我在如下所示的数组中有两个对象:

[{"Footprint_Shape":["L-Shape","H-Shape","T-Shape"]},
{"Num_of_Floors":[1,2]}]

我想生成下面的数组,它是每个组合,同时保留键:

    [{"Footprint_Shape": "L-Shape", "Num_of_Floors":1 },
    { "Footprint_Shape": "H-Shape", "Num_of_Floors":1 },
    { "Footprint_Shape": "T-Shape", "Num_of_Floors":1 },
    { "Footprint_Shape": "L-Shape", "Num_of_Floors":2 },
    { "Footprint_Shape": "H-Shape", "Num_of_Floors":2 },
    { "Footprint_Shape": "T-Shape", "Num_of_Floors":2 }]

请记住,我需要动态生成所有键和值。

任何能指出我编写此代码的正确方向的指针或代码示例将不胜感激

【问题讨论】:

  • 通过简单的嵌套循环轻松实现。

标签: javascript algorithm dictionary combinations permutation


【解决方案1】:

您可以将对象数组转换为多维数组。构造可能的组合并使用map 构造最终格式。

var arr = [{"Footprint_Shape": ["L-Shape", "H-Shape", "T-Shape"]}, {"Num_of_Floors": [1, 2]}];

var result = arr.map(o => Object.values(o)[0])                               //Convert the array of objects into multi dimensional array.
  .reduce((c, v) => [].concat(...c.map(o => v.map(x => [].concat(o, x)))))   //Make all possible combinations
  .map(([a, b]) => ({"Footprint_Shape": a,"Num_of_Floors": b}))              //Construct the final format

console.log(result);

更新:

var arr = [{"Footprint_Shape": ["L-Shape", "H-Shape", "T-Shape"]}, {"Num_of_Floors": [1, 2]}];

var keys = arr.map(o => Object.keys(o)[0]); //Get the list of keys
var result = arr.map(o => Object.values(o)[0]) //Convert the array of objects into multi dimensional array.
  .reduce((c, v) => [].concat(...c.map(o => v.map(x => [].concat(o, x))))) //Make all possible combinations
  .map(o => o.reduce((c, v, i) => Object.assign(c, {[keys[i]]: v}), {})); //Construct the final format

console.log(result);

【讨论】:

  • 感谢您的回答,但是我需要能够动态执行此操作,例如 - 我不知道对象的数量、数组的长度或每个对象中的键名提前
  • @AntonJames 请检查更新的代码。这是动态的。
【解决方案2】:

一个简单而简短的替代方案:

const [{Footprint_Shape: shapes},{Num_of_Floors: floors} ] = [{"Footprint_Shape":["L-Shape","H-Shape","T-Shape"]},{"Num_of_Floors":[1,2]}];

const result = floors.reduce((all, floor) => {

    shapes.forEach(shape => all.push({Footprint_Shape: shape, Num_of_Floors: floor}))

    return all;

}, []);

console.log(result);

【讨论】:

    【解决方案3】:

    let arr = [
      {"Footprint_Shape":["L-Shape","H-Shape","T-Shape"]},
      {"Num_of_Floors":[1,2]}
    ]
    
    let answer = [];
    
    arr[0]["Footprint_Shape"].forEach(x => {  
      
      arr[1]["Num_of_Floors"].forEach(y => {
        
        let newObj = {};  
        newObj["Footprint_Shape"] = x;
        //console.log('y: ',y)
        newObj["Num_of_Floors"] = y
      	answer.push(newObj);
        
      })
    });
    
    console.log(answer)

    我认为代码应该是不言自明的。使用两次循环,构造对象并推送到新数组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-25
      • 2021-10-31
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多