Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从动态属性创建所有可能的组合?_Javascript - Fatal编程技术网

Javascript 如何从动态属性创建所有可能的组合?

Javascript 如何从动态属性创建所有可能的组合?,javascript,Javascript,我需要从以下属性列表中生成所有可能的AttributeValue组合 attributes:[ { attributeName:"Color of leg", attributeValues:[blue,green,black] }, { attributeName:"Color of hand", attributeValues:[blue,orange,white] }, { attributeName:"Color of nose", attributeV

我需要从以下属性列表中生成所有可能的AttributeValue组合

attributes:[
 {
  attributeName:"Color of leg",
  attributeValues:[blue,green,black]
 },
 {
  attributeName:"Color of hand",
  attributeValues:[blue,orange,white]
 },
 {
  attributeName:"Color of nose",
  attributeValues:[red,orange]
 }
]
列表中attributeName和相关AttributeValue的出现次数可能会有所不同,因此“for的固定深度”不适用。我现在挣扎了很长时间,无法前进。我不知道该怎么处理

预期结果是:

object=[{colorOfLeg:"blue",colorOfHand:"blue",colorOfNose:"red"},
        {colorOfLeg:"blue",colorOfHand:"blue",colorOfNose:"orange"},
        {colorOfLeg:"green",colorOfHand:"blue",colorOfNose:"red"},
        etc...
       ]
属性列表的外观不是必须保留的。 谢谢你的帮助

*****************************更新

数据的输入格式更改为:

 dynamicAttributes:[
     {attrName:"Color of hand",attrValue:"blue",index:1}, 
     {attrName:"Color of hand",attrValue:"orange",index:2},
     {attrName:"Color of hand",attrValue:"purple",index:3},

     {attrName:"Color of nose",attrValue:"blue",index:4},   
     {attrName:"Color of nose",attrValue:"white",index:5},

     {attrName:"Color of leg",attrValue:"blue",index:6},   
     {attrName:"Color of leg",attrValue:"white",index:7}
    ]
之后,我找到了所有唯一的attrName(腿的颜色、鼻子的颜色、手的颜色)。从唯一的name中,我生成了数组列表,其中每个数组都包含按唯一attrName分组的索引

indexGroups=[[1,2,3],[4,5],[6,7]]
我想我要找的是“笛卡尔积”。我可以想象“静态逻辑”,如果唯一attrName的数量是连续的,但不知道如何为这种情况创建动态逻辑

*****************************更新2

我在另一个stackoverflow线程中找到该函数:

const f = (a, b) => [].concat(...a.map(d => b.map(e => [].concat(d, e))));
const cartesian = (a, b, ...c) => (b ? cartesian(f(a, b), ...c) : a);
对所有数组调用“笛卡尔”将产生所有可能的组合

cartesian([1,2,3],[4,5],[6,7])

我完全不明白这个功能。有人能解释一下吗?特别是f函数

尝试使用nested.map。第一级遍历属性列表中的项,然后下一级.map遍历AttributeValue。看看MDN的一些例子。你好,Teej,anwer的thx。如果我的回答是正确的,那么提交将返回attributeName和所有AttributeValue,但如何将所有AttributeValue(从鼻子颜色、腿颜色等)组合在一起?