具有笛卡尔实现的树递归

问题描述 投票:1回答:2

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

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

这是我的树

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"},
  ]
]

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

感谢您的帮助:)

javascript tree cartesian-product cartesian-tree
2个回答
1
投票

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

function getCartesian(array) {
    return array.reduce((r, { children = [], ...o }) => {
        if (children.length) {
            r.push(...getCartesian(children).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: 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 = getCartesian(data);

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

0
投票

thx花花公子,这是一个不错的开始,但效果并不理想。这是我的错,我没有发布太多复杂的json。但是有了这个,就不符合我的预期结果。我需要按过滤器对一些孩子进行分组...

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": []
              }
            ]
          }
        ]
      }
    ]
  }
]

预期输出:

[
  [
    {id: 5, name:"support papier", type: "filter"}, 
    {id: 24, name:"60 g/m² papier mat", type: "value"},
    {id: 9, name:"Finition", type: "filter"}, 
    {id: 19, name:"Sans finition", 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: 24, name:"60 g/m² papier mat", type: "value"},
    {id: 9, name:"Finition", type: "filter"}, 
    {id: 19, name:"Sans finition", type: "value"},
    {id: 8, name:"Propriété papier", type: "filter"}, 
    {id: 60, name:"Papier brillant", type: "value"},

  ],
  [
    {id: 5, name:"support papier", type: "filter"}, 
    {id: 24, name:"60 g/m² papier mat", type: "value"},
    {id: 9, name:"Finition", type: "filter"}, 
    {id: 20, name:"Vernis anti UV", 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: 24, name:"60 g/m² papier mat", type: "value"},
    {id: 9, name:"Finition", type: "filter"}, 
    {id: 20, name:"Vernis anti UV", type: "value"},
    {id: 8, name:"Propriété papier", type: "filter"}, 
    {id: 60, name:"Papier brillant", 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"},
  ]
]
© www.soinside.com 2019 - 2024. All rights reserved.