简体   繁体   English

将对象数组过滤到数据树中

[英]filter array of objects into tree of data

In my reducer, I am trying to filter an array of objects (categories), parent categories have parent: 0 , all other categories have a parent id. 在我的reducer中,我尝试过滤对象(类别)的数组,父类别的parent: 0 ,所有其他类别的父级ID为。 Categories are nested 3 deep max. 类别嵌套最多3个深度。

I would like to store the subcategories in an options array. 我想将子类别存储在options数组中。 I have this code below. 我下面有这段代码。 I can't get any deep nested options, any ideas? 我找不到任何深层嵌套的选项,有什么主意吗?

        // get top level categories
        const yearCategories = action.data.filter(
            category => category.parent == 0
        );

        const categorys = yearCategories.map(({ id, name: label }) => ({
            id,
            label,
            options: action.data
                .filter(({ parent }) => parent === id)
                .map(({ id, name: label }) => ({
                    id,
                    label,
                    options: action.data
                        .filter(({ subCategory }) => subCategory === id)
                        .map(({ id, name: label }) => ({ id, label }))
                }))
        }));

Sample json, from http://wordpress.rguc.co.uk/index.php/wp-json/tribe/events/v1/categories?per_page=60&page=1 示例json,来自http://wordpress.rguc.co.uk/index.php/wp-json/tribe/events/v1/categories?per_page=60&page=1

      [
{"id":1,"parent":0,"description":"","name":"Year 3"}
{"id":2,"parent":0,"description":"","name":"Year 4"}
{"id":3,"parent":0,"description":"","name":"Year 5"}
{"id":4,"parent":1,"description":"","name":"Year 3 group"}
{"id":5,"parent":1,"description":"","name":"Year 3 group"}
{"id":6,"parent":2,"description":"","name":"Year 4 group"}
{"id":7,"parent":2,"description":"","name":"Year 4 group"}
{"id":8,"parent":3,"description":"","name":"Year 5 Group"}
{"id":9,"parent":3,"description":"","name":"Year 5 Group"}
{"id":10,"parent":4,"description":"","name":"Year 3 group student"}
{"id":11,"parent":4,"description":"","name":"Year 3 group student"}
{"id":12,"parent":5,"description":"","name":"Year 3 group student"}
{"id":13,"parent":5,"description":"","name":"Year 3 group student"}
{"id":14,"parent":6,"description":"","name":"Year 4 group student"}
{"id":15,"parent":6,"description":"","name":"Year 4 group student"}
]

Which I want to look like this, example for Year 3 : 我想看起来像这样,例如Year 3

{
  "id": 1,
    "name": "Year 3",
    "parent": 0,
  "options": [
    {
      "id": 4,
      "parent": 1,
      "name": "Year 3 group",
      "options": [
        {
          "id": 10,
          "parent": 4,
          "name": "Year 3 group student"
        },
        {
          "id": 11,
          "parent": 4,
          "name": "Year 3 group student"
        }
      ]
      },
    {
      "id": 5,
      "parent": 1,
      "name": "Year 3 group",
      "option":[]
    }
  ]}

You could take an object as reference to the nodes and an array for storing all nodes who have a parent property with zero, because this nodes vae no parent. 您可以将一个对象作为节点的参考,并使用一个数组来存储所有父属性为零的节点,因为该节点无父。

This solution works by iterating all nodes once and sortes beside the id as refernece of a node the parent as well. 该解决方案通过迭代所有节点一次,并在id旁边作为父节点的参考引用进行排序。 At the end all connected nodes are collected and in the right order. 最后,以正确的顺序收集所有连接的节点。

This works for unsorted data, because of the object and the relation between nodes and their parents and parents and their childrens. 这适用于未排序的数据,因为节点及其父级与父级及其子级之间的对象和关系。

 var data = [{ id: 1, parent: 0, description: "", name: "Year 3" }, { id: 2, parent: 0, description: "", name: "Year 4" }, { id: 3, parent: 0, description: "", name: "Year 5" }, { id: 4, parent: 1, description: "", name: "Year 3 group" }, { id: 5, parent: 1, description: "", name: "Year 3 group" }, { id: 6, parent: 2, description: "", name: "Year 4 group" }, { id: 7, parent: 2, description: "", name: "Year 4 group" }, { id: 8, parent: 3, description: "", name: "Year 5 Group" }, { id: 9, parent: 3, description: "", name: "Year 5 Group" }, { id: 10, parent: 4, description: "", name: "Year 3 group student" }, { id: 11, parent: 4, description: "", name: "Year 3 group student" }, { id: 12, parent: 5, description: "", name: "Year 3 group student" }, { id: 13, parent: 5, description: "", name: "Year 3 group student" }, { id: 14, parent: 6, description: "", name: "Year 4 group student" }, { id: 15, parent: 6, description: "", name: "Year 4 group student" }], tree = function (data, root) { var r = [], o = {}; data.forEach(function (a) { a.options = o[a.id] && o[a.id].options; o[a.id] = a; if (a.parent === root) { r.push(a); } else { o[a.parent] = o[a.parent] || {}; o[a.parent].options = o[a.parent].options || []; o[a.parent].options.push(a); } }); return r; }(data, 0); console.log(tree); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM