【发布时间】: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