Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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_Algorithm_Recursion - Fatal编程技术网

Javascript 获取嵌套对象结构字段的所有排列

Javascript 获取嵌套对象结构字段的所有排列,javascript,algorithm,recursion,Javascript,Algorithm,Recursion,我有以下对象,这只是一个示例,因为深度可以超过深度,所有NEXT中的对象可能超过1个,目标是提取给定结构的所有VAL的排列: const config = { Next: [ { Val: 11, Next: [ { Val: 21, Next: [ { Val: 31, Next: [ {

我有以下对象,这只是一个示例,因为深度可以超过深度,所有NEXT中的对象可能超过1个,目标是提取给定结构的所有VAL的排列:

const config = {
 Next: [
    {
      Val: 11,
      Next: [
        {
          Val: 21,

          Next: [
            {
              Val: 31,
              Next: [
                {
                  Val: 42,
                },
                {
                  Val: 43,
                },
              ],
            },
            {
              Val: 32,
              Next: [
                {
                  Val: 41,
                },
                {
                  Val: 42,
                },
                {
                  Val: 43,
                },
              ],
            },
          ],
        },
      ],
    },
  ]
}
输出应该是数组的数组,并且顺序很重要:

[
  [11, 21, 31, 42],
  [11, 21, 31, 43],
  [11, 21, 32, 41],
  [11, 21, 32, 42],
  [11, 21, 32, 43]
]
我已尝试实现某种递归,但无法获得正确的输出:

let combinations = [];

const getNextItems = (nextItems) => {
  let subarray = [];

  nextItems.Next.forEach(item => {
    subarray.push(item.Val);
    item.Next && getNextItems(item)
  });
 
  combinations.push(subarray);
}

getNextItems(config);   

console.log(combinations);
相反,我得到:

[
  [42, 43],
  [41, 42, 43],
  [31, 32],
  [21],
  [11],
]
我感觉到我在正确的轨道上,因为这些是正确的值,只是它没有排列,而是给我每个级别的所有项目的数组

编辑:将子数组移出递归后,我得到了第一个正确的排列,但没有得到其余的排列:

let combinations = [];
let subarray = [];

const getNextItems = (nextItems) => {    
  nextItems.Next.forEach(item => {
    subarray.push(item.Val);
    item.Next && getNextItems(item)
    combinations[robotName].push(subarray);
    subarray = [];
  });  
}
结果是:

[
  [11, 21, 31, 42],
  [43],
  [],
  [32, 41],
  [42],
  [43],
  [],
  [],
  [],
]

有一种模式在某种程度上给了我排列方式,但不是我想要的方式:你可以采取迭代和递归的方法,收集所有深度的所有值

获取数组并执行具有临时结果的操作。在本例中,startValue是一个数组。对于数组的每个元素,它都会使用Val和Next的值,并检查是否存在嵌套数组

如果是,则从该数组中获取值,并返回子项。要将新数组添加到结果集中,还需要添加实际的Val

如果不是,只需向结果集添加一个包装的Val

常数 getValues=array=>array.reducer,{Val,Next}=>{ 如果下一个r.push…getValuesNext.mapa=>[Val,…a]; else r.push[Val]; 返回r; }, [], config={Next:[{Val:11,Next:[{Val:21,Next:[{Val:31,Next:[{Val:42},{Val:43},],},{Val:32,Next:[{Val:41},{Val:42},{Val:43}]}, 结果=getValuesconfig.Next;
result.forEacha=>console.log…a 您可以采用迭代和递归方法,从所有深度收集所有值

获取数组并执行具有临时结果的操作。在本例中,startValue是一个数组。对于数组的每个元素,它都会使用Val和Next的值,并检查是否存在嵌套数组

如果是,则从该数组中获取值,并返回子项。要将新数组添加到结果集中,还需要添加实际的Val

如果不是,只需向结果集添加一个包装的Val

常数 getValues=array=>array.reducer,{Val,Next}=>{ 如果下一个r.push…getValuesNext.mapa=>[Val,…a]; else r.push[Val]; 返回r; }, [], config={Next:[{Val:11,Next:[{Val:21,Next:[{Val:31,Next:[{Val:42},{Val:43},],},{Val:32,Next:[{Val:41},{Val:42},{Val:43}]}, 结果=getValuesconfig.Next;
result.forEacha=>console.log…a 这可以通过简单的递归和flatMap调用来完成。这里有一个方法:

const getCombos={Val,Next},path=[]=> 瓦尔&!下一个 ? […路径,Val]] :Next | |[].flatMap o=>getCombos o,[…路径,…Val?[Val]:[] const config={Next:[{Val:11,Next:[{Val:21,Next:[{Val:31,Next:[{Val:42},{Val:43}]},{Val:32,Next:[{Val:41},{Val:42},{Val:43}]}} log getCombos配置
.as console wrapper{max height:100%!important;top:0}这可以通过简单的递归和flatMap调用来完成。这里有一个方法:

const getCombos={Val,Next},path=[]=> 瓦尔&!下一个 ? […路径,Val]] :Next | |[].flatMap o=>getCombos o,[…路径,…Val?[Val]:[] const config={Next:[{Val:11,Next:[{Val:21,Next:[{Val:31,Next:[{Val:42},{Val:43}]},{Val:32,Next:[{Val:41},{Val:42},{Val:43}]}} log getCombos配置
.作为控制台包装{max height:100%!important;top:0}我已经想出了一个新函数来完成上述任务

该函数的工作原理是从子对象收集组合,然后为当前对象形成新组合并返回相同的组合

让组合=[]; const getcombines=parentObject=>{ var childCombinationList=[]; ifparentObject.Next!=未定义{ parentObject.Next.forEachitem=>{ childCombinationList=childCombinationList.concatgetCombinationsitem; }; } ifparentObject.Val!=未定义{ var newcommbinationlist=[]; ifchildCombinationList.length>0{ childCombinationList.forEachchildCombination=>{ var newCombination=[parentObject.Val]; newCombination=newCombination.concatchildComposition; newCombinationList.pushnewCombination; }; } 否则{ newCombinationList=[[parentObject.Val]]; } 返回newCombinationList; } 否则{ 返回子组合列表; } } config={Next:[{Val:11,Next:[{Val:21,Next:[{Val:31,Next:[{Val:42},{Val:43},],},{Val:32,Next:[{Val:41},{Val:42},{Val:43}]}, 组合=getCombinationsconfig; logco公司
联合国 我已经想出了一个新的功能来完成上述任务

该函数的工作原理是从子对象收集组合,然后为当前对象形成新组合并返回相同的组合

让组合=[]; const getcombines=parentObject=>{ var childCombinationList=[]; ifparentObject.Next!=未定义{ parentObject.Next.forEachitem=>{ childCombinationList=childCombinationList.concatgetCombinationsitem; }; } ifparentObject.Val!=未定义{ var newcommbinationlist=[]; ifchildCombinationList.length>0{ childCombinationList.forEachchildCombination=>{ var newCombination=[parentObject.Val]; newCombination=newCombination.concatchildComposition; newCombinationList.pushnewCombination; }; } 否则{ newCombinationList=[[parentObject.Val]]; } 返回newCombinationList; } 否则{ 返回子组合列表; } } config={Next:[{Val:11,Next:[{Val:21,Next:[{Val:31,Next:[{Val:42},{Val:43},],},{Val:32,Next:[{Val:41},{Val:42},{Val:43}]}, 组合=getCombinationsconfig;
控制台。日志组合;但是我不太熟悉。reduce还没有,你能简单地解释一下代码是如何工作的吗?@Darkbound,请看一看。但是我不太熟悉。reduce还没有,你能简单地解释一下代码是如何工作的吗?@Darkbound,请看一看。