【问题标题】:tree recursive with cartesian implementation具有笛卡尔实现的树递归
【发布时间】:2020-03-01 16:33:50
【问题描述】:

我需要帮助才能从树中找到所有组合的可能性

我阅读了很多关于笛卡尔积的文档,尝试了很多东西,但似乎都没有正常工作......

这是我的树

var data = [
  {
    "id": 5,
    "name": "Support papier",
    "type": "filter",
    "children": [
      {
        "id": 24,
        "name": "60 g/m² papier mat",
        "type": "value",
        "children": []
      },
      {
        "id": 7,
        "name": "90 g/m² papier couché",
        "type": "value",
        "children": [
          {
            "id": 8,
            "name": "Propriété papier",
            "type": "filter",
            "children": [
              {
                "id": 18,
                "name": "Papier mat",
                "type": "value",
                "children": [],
              },
              {
                "id": 60,
                "name": "Papier brillant",
                "type": "value",
                "children": [],
              }
            ]
          }
        ],
      }
    ]
  }
]

这是我的预期结果:

[
  [
    {id: 5, name:"support papier", type: "filter"}, 
    {id: 24, name:"60 g/m² papier mat", type: "value"},

  ],
  [
    {id: 5, name:"support papier", type: "filter"}, 
    {id: 7, name:"90 g/m² papier mat", type: "value"},
    {id: 8, name:"Propriété papier", type: "filter"}, 
    {id: 18, name:"Papier mat", type: "value"},
  ],
  [
    {id: 5, name:"support papier", type: "filter"}, 
    {id: 7, name:"90 g/m² papier mat", type: "value"},
    {id: 8, name:"Propriété papier", type: "filter"}, 
    {id: 60, name:"Papier brillant", type: "value"},
  ]
]

当然,每个空数组都可以填充... :)

感谢您的帮助:)

【问题讨论】:

  • 在输出的第二和第三项中应该是id: 7而不是id: 24
  • 是的,你是对的 ;)

标签: javascript tree cartesian-product cartesian-tree


【解决方案1】:

您可以获取每个级别并将下一个较低级别映射到结果集。

现在,它在做什么?

收集数据的第一部分只是让所有节点到孩子对象的末尾。

另一部分是使用对象具有的孩子的笛卡尔积

type === 'value'

这分两步进行

  1. 通过获取数据收集所有项目并将这些项目与实际对象映射。

  2. 从一组项目中创建一个cartesian product

剩下的只是使用parts 推送一个新数组并添加实际对象(没有子对象),或者如果没有可用的子对象,则只有数组中的实际对象。

function getData(array) {
    return array.reduce((r, { children, ...o }) => {
        if (children.length) {
            var parts = o.type === 'value'
                    ? children
                        .map(({ children = [], ...p }) => getData(children).map(q => [p, ...q]))
                        .reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []))
                    : getData(children);

            r.push(...parts.map(q => [o, ...q]));
        } else {
            r.push([o]);
        }
        return r;
    }, []);
}

var data = [{ id: 5, name: "Support papier", type: "filter", children: [{ id: 24, name: "60 g/m² papier mat", type: "value", children: [{ id: 9, name: "Finition", type: "filter", children: [{ id: 19, name: "Sans finition", type: "value", children: [] }, { id: 20, name: "Vernis anti UV", type: "value", children: [] }] }, { id: 8, name: "Propriété papier", type: "filter", children: [{ id: 60, name: "Papier brillant", type: "value", children: [] }, { id: 18, name: "Papier mat", type: "value", children: [] }] }] }, { id: 7, name: "90 g/m² papier couché", type: "value", children: [{ id: 8, name: "Propriété papier", type: "filter", children: [{ id: 18, name: "Papier mat", type: "value", children: [] }, { id: 60, name: "Papier brillant", type: "value", children: [] }] }] }] }],
    result = getData(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

    猜你喜欢
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2021-05-06
    • 2011-10-20
    • 2022-01-19
    • 2013-05-26
    • 2020-09-15
    相关资源
    最近更新 更多