过滤嵌套对象

时间:2020-02-24 17:25:28

标签: javascript arrays node.js

我正在尝试过滤类别,我想要产品类别1.1的子项,但是atm我正在获得纸箱的子项。我在这里做错了什么?谢谢!

let categories =         [
    {
    "productCategoryLevel1": "1",
    "productCategoryLevel1Description": "Cartons",
    "sortingKeyLevel1": 1,
    "children": [
    {
    "productCategoryLevel2": "1.1",
    "productCategoryLevel2Description": "Product Category 1.1",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.1.1",
    "productCategoryLevel3Description": "Product Category 1.1.1",
    "productCategoryLevel2": "1.1",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "1.1.2",
    "productCategoryLevel3Description": "Product Category 1.1.2",
    "productCategoryLevel2": "1.1",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.1.3",
    "productCategoryLevel3Description": "Product Category 1.1.3",
    "productCategoryLevel2": "1.1",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "1.1.4",
    "productCategoryLevel3Description": "Product Category 1.1.4",
    "productCategoryLevel2": "1.1",
    "categoryCount": 2
    }
    ]
    },
    {
    "productCategoryLevel2": "1.2",
    "productCategoryLevel2Description": "Product Category 1.2",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.2.1",
    "productCategoryLevel3Description": "Product Category 1.2.1",
    "productCategoryLevel2": "1.2",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "1.2.2",
    "productCategoryLevel3Description": "Product Category 1.2.2",
    "productCategoryLevel2": "1.2",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.2.3",
    "productCategoryLevel3Description": "Product Category 1.2.3",
    "productCategoryLevel2": "1.2",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.2.4",
    "productCategoryLevel3Description": "Product Category 1.2.4",
    "productCategoryLevel2": "1.2",
    "categoryCount": 3
    }
    ]
    },
    {
    "productCategoryLevel2": "1.3",
    "productCategoryLevel2Description": "Product Category 1.3",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.3.1",
    "productCategoryLevel3Description": "Product Category 1.3.1",
    "productCategoryLevel2": "1.3",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.3.2",
    "productCategoryLevel3Description": "Product Category 1.3.2",
    "productCategoryLevel2": "1.3",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.3.3",
    "productCategoryLevel3Description": "Product Category 1.3.3",
    "productCategoryLevel2": "1.3",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.3.4",
    "productCategoryLevel3Description": "Product Category 1.3.4",
    "productCategoryLevel2": "1.3",
    "categoryCount": 0
    }
    ]
    },
    {
    "productCategoryLevel2": "1.4",
    "productCategoryLevel2Description": "Product Category 1.4",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.4.1",
    "productCategoryLevel3Description": "Product Category 1.4.1",
    "productCategoryLevel2": "1.4",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.4.2",
    "productCategoryLevel3Description": "Product Category 1.4.2",
    "productCategoryLevel2": "1.4",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.4.3",
    "productCategoryLevel3Description": "Product Category 1.4.3",
    "productCategoryLevel2": "1.4",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.4.4",
    "productCategoryLevel3Description": "Product Category 1.4.4",
    "productCategoryLevel2": "1.4",
    "categoryCount": 0
    }
    ]
    },
    {
    "productCategoryLevel2": "1.5",
    "productCategoryLevel2Description": "Product Category 1.5",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.5.1",
    "productCategoryLevel3Description": "Product Category 1.5.1",
    "productCategoryLevel2": "1.5",
    "categoryCount": 4
    },
    {
    "productCategoryLevel3": "1.5.2",
    "productCategoryLevel3Description": "Product Category 1.5.2",
    "productCategoryLevel2": "1.5",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.5.3",
    "productCategoryLevel3Description": "Product Category 1.5.3",
    "productCategoryLevel2": "1.5",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "1.5.4",
    "productCategoryLevel3Description": "Product Category 1.5.4",
    "productCategoryLevel2": "1.5",
    "categoryCount": 0
    }
    ]
    },
    {
    "productCategoryLevel2": "1.6",
    "productCategoryLevel2Description": "Product Category 1.6",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.6.1",
    "productCategoryLevel3Description": "Product Category 1.6.1",
    "productCategoryLevel2": "1.6",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.6.2",
    "productCategoryLevel3Description": "Product Category 1.6.2",
    "productCategoryLevel2": "1.6",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "1.6.3",
    "productCategoryLevel3Description": "Product Category 1.6.3",
    "productCategoryLevel2": "1.6",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.6.4",
    "productCategoryLevel3Description": "Product Category 1.6.4",
    "productCategoryLevel2": "1.6",
    "categoryCount": 4
    }
    ]
    },
    {
    "productCategoryLevel2": "1.7",
    "productCategoryLevel2Description": "Product Category 1.7",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.7.1",
    "productCategoryLevel3Description": "Product Category 1.7.1",
    "productCategoryLevel2": "1.7",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "1.7.2",
    "productCategoryLevel3Description": "Product Category 1.7.2",
    "productCategoryLevel2": "1.7",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.7.3",
    "productCategoryLevel3Description": "Product Category 1.7.3",
    "productCategoryLevel2": "1.7",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "1.7.4",
    "productCategoryLevel3Description": "Product Category 1.7.4",
    "productCategoryLevel2": "1.7",
    "categoryCount": 2
    }
    ]
    },
    {
    "productCategoryLevel2": "1.8",
    "productCategoryLevel2Description": "Product Category 1.8",
    "productCategoryLevel1": "1",
    "children": [
    {
    "productCategoryLevel3": "1.8.1",
    "productCategoryLevel3Description": "Product Category 1.8.1",
    "productCategoryLevel2": "1.8",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.8.2",
    "productCategoryLevel3Description": "Product Category 1.8.2",
    "productCategoryLevel2": "1.8",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "1.8.3",
    "productCategoryLevel3Description": "Product Category 1.8.3",
    "productCategoryLevel2": "1.8",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "1.8.4",
    "productCategoryLevel3Description": "Product Category 1.8.4",
    "productCategoryLevel2": "1.8",
    "categoryCount": 2
    }
    ]
    }
    ]
    },
    {
    "productCategoryLevel1": "2",
    "productCategoryLevel1Description": "Films",
    "sortingKeyLevel1": 2,
    "children": [
    {
    "productCategoryLevel2": "2.1",
    "productCategoryLevel2Description": "Product Category 2.1",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.1.1",
    "productCategoryLevel3Description": "Product Category 2.1.1",
    "productCategoryLevel2": "2.1",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.1.2",
    "productCategoryLevel3Description": "Product Category 2.1.2",
    "productCategoryLevel2": "2.1",
    "categoryCount": 5
    },
    {
    "productCategoryLevel3": "2.1.3",
    "productCategoryLevel3Description": "Product Category 2.1.3",
    "productCategoryLevel2": "2.1",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.1.4",
    "productCategoryLevel3Description": "Product Category 2.1.4",
    "productCategoryLevel2": "2.1",
    "categoryCount": 0
    }
    ]
    },
    {
    "productCategoryLevel2": "2.2",
    "productCategoryLevel2Description": "Product Category 2.2",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.2.1",
    "productCategoryLevel3Description": "Product Category 2.2.1",
    "productCategoryLevel2": "2.2",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.2.2",
    "productCategoryLevel3Description": "Product Category 2.2.2",
    "productCategoryLevel2": "2.2",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "2.2.3",
    "productCategoryLevel3Description": "Product Category 2.2.3",
    "productCategoryLevel2": "2.2",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.2.4",
    "productCategoryLevel3Description": "Product Category 2.2.4",
    "productCategoryLevel2": "2.2",
    "categoryCount": 2
    }
    ]
    },
    {
    "productCategoryLevel2": "2.3",
    "productCategoryLevel2Description": "Product Category 2.3",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.3.1",
    "productCategoryLevel3Description": "Product Category 2.3.1",
    "productCategoryLevel2": "2.3",
    "categoryCount": 4
    },
    {
    "productCategoryLevel3": "2.3.2",
    "productCategoryLevel3Description": "Product Category 2.3.2",
    "productCategoryLevel2": "2.3",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.3.3",
    "productCategoryLevel3Description": "Product Category 2.3.3",
    "productCategoryLevel2": "2.3",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.3.4",
    "productCategoryLevel3Description": "Product Category 2.3.4",
    "productCategoryLevel2": "2.3",
    "categoryCount": 2
    }
    ]
    },
    {
    "productCategoryLevel2": "2.4",
    "productCategoryLevel2Description": "Product Category 2.4",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.4.1",
    "productCategoryLevel3Description": "Product Category 2.4.1",
    "productCategoryLevel2": "2.4",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.4.2",
    "productCategoryLevel3Description": "Product Category 2.4.2",
    "productCategoryLevel2": "2.4",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "2.4.3",
    "productCategoryLevel3Description": "Product Category 2.4.3",
    "productCategoryLevel2": "2.4",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.4.4",
    "productCategoryLevel3Description": "Product Category 2.4.4",
    "productCategoryLevel2": "2.4",
    "categoryCount": 1
    }
    ]
    },
    {
    "productCategoryLevel2": "2.5",
    "productCategoryLevel2Description": "Product Category 2.5",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.5.1",
    "productCategoryLevel3Description": "Product Category 2.5.1",
    "productCategoryLevel2": "2.5",
    "categoryCount": 4
    },
    {
    "productCategoryLevel3": "2.5.2",
    "productCategoryLevel3Description": "Product Category 2.5.2",
    "productCategoryLevel2": "2.5",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.5.3",
    "productCategoryLevel3Description": "Product Category 2.5.3",
    "productCategoryLevel2": "2.5",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "2.5.4",
    "productCategoryLevel3Description": "Product Category 2.5.4",
    "productCategoryLevel2": "2.5",
    "categoryCount": 0
    }
    ]
    },
    {
    "productCategoryLevel2": "2.6",
    "productCategoryLevel2Description": "Product Category 2.6",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.6.1",
    "productCategoryLevel3Description": "Product Category 2.6.1",
    "productCategoryLevel2": "2.6",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "2.6.2",
    "productCategoryLevel3Description": "Product Category 2.6.2",
    "productCategoryLevel2": "2.6",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.6.3",
    "productCategoryLevel3Description": "Product Category 2.6.3",
    "productCategoryLevel2": "2.6",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "2.6.4",
    "productCategoryLevel3Description": "Product Category 2.6.4",
    "productCategoryLevel2": "2.6",
    "categoryCount": 2
    }
    ]
    },
    {
    "productCategoryLevel2": "2.7",
    "productCategoryLevel2Description": "Product Category 2.7",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.7.1",
    "productCategoryLevel3Description": "Product Category 2.7.1",
    "productCategoryLevel2": "2.7",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "2.7.2",
    "productCategoryLevel3Description": "Product Category 2.7.2",
    "productCategoryLevel2": "2.7",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.7.3",
    "productCategoryLevel3Description": "Product Category 2.7.3",
    "productCategoryLevel2": "2.7",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "2.7.4",
    "productCategoryLevel3Description": "Product Category 2.7.4",
    "productCategoryLevel2": "2.7",
    "categoryCount": 3
    }
    ]
    },
    {
    "productCategoryLevel2": "2.8",
    "productCategoryLevel2Description": "Product Category 2.8",
    "productCategoryLevel1": "2",
    "children": [
    {
    "productCategoryLevel3": "2.8.1",
    "productCategoryLevel3Description": "Product Category 2.8.1",
    "productCategoryLevel2": "2.8",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "2.8.2",
    "productCategoryLevel3Description": "Product Category 2.8.2",
    "productCategoryLevel2": "2.8",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "2.8.3",
    "productCategoryLevel3Description": "Product Category 2.8.3",
    "productCategoryLevel2": "2.8",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "2.8.4",
    "productCategoryLevel3Description": "Product Category 2.8.4",
    "productCategoryLevel2": "2.8",
    "categoryCount": 3
    }
    ]
    }
    ]
    },
    {
    "productCategoryLevel1": "3",
    "productCategoryLevel1Description": "Protection",
    "sortingKeyLevel1": 3,
    "children": [
    {
    "productCategoryLevel2": "3.1",
    "productCategoryLevel2Description": "Product Category 3.1",
    "productCategoryLevel1": "3",
    "children": [
    {
    "productCategoryLevel3": "3.1.1",
    "productCategoryLevel3Description": "Product Category 3.1.1",
    "productCategoryLevel2": "3.1",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "3.1.2",
    "productCategoryLevel3Description": "Product Category 3.1.2",
    "productCategoryLevel2": "3.1",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "3.1.3",
    "productCategoryLevel3Description": "Product Category 3.1.3",
    "productCategoryLevel2": "3.1",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "3.1.4",
    "productCategoryLevel3Description": "Product Category 3.1.4",
    "productCategoryLevel2": "3.1",
    "categoryCount": 3
    }
    ]
    },
    {
    "productCategoryLevel2": "3.2",
    "productCategoryLevel2Description": "Product Category 3.2",
    "productCategoryLevel1": "3",
    "children": [
    {
    "productCategoryLevel3": "3.2.1",
    "productCategoryLevel3Description": "Product Category 3.2.1",
    "productCategoryLevel2": "3.2",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "3.2.2",
    "productCategoryLevel3Description": "Product Category 3.2.2",
    "productCategoryLevel2": "3.2",
    "categoryCount": 4
    },
    {
    "productCategoryLevel3": "3.2.3",
    "productCategoryLevel3Description": "Product Category 3.2.3",
    "productCategoryLevel2": "3.2",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "3.2.4",
    "productCategoryLevel3Description": "Product Category 3.2.4",
    "productCategoryLevel2": "3.2",
    "categoryCount": 1
    }
    ]
    },
    {
    "productCategoryLevel2": "3.3",
    "productCategoryLevel2Description": "Product Category 3.3",
    "productCategoryLevel1": "3",
    "children": [
    {
    "productCategoryLevel3": "3.3.1",
    "productCategoryLevel3Description": "Product Category 3.3.1",
    "productCategoryLevel2": "3.3",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "3.3.2",
    "productCategoryLevel3Description": "Product Category 3.3.2",
    "productCategoryLevel2": "3.3",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "3.3.3",
    "productCategoryLevel3Description": "Product Category 3.3.3",
    "productCategoryLevel2": "3.3",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "3.3.4",
    "productCategoryLevel3Description": "Product Category 3.3.4",
    "productCategoryLevel2": "3.3",
    "categoryCount": 1
    }
    ]
    },
    {
    "productCategoryLevel2": "3.4",
    "productCategoryLevel2Description": "Product Category 3.4",
    "productCategoryLevel1": "3",
    "children": [
    {
    "productCategoryLevel3": "3.4.1",
    "productCategoryLevel3Description": "Product Category 3.4.1",
    "productCategoryLevel2": "3.4",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "3.4.2",
    "productCategoryLevel3Description": "Product Category 3.4.2",
    "productCategoryLevel2": "3.4",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "3.4.3",
    "productCategoryLevel3Description": "Product Category 3.4.3",
    "productCategoryLevel2": "3.4",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "3.4.4",
    "productCategoryLevel3Description": "Product Category 3.4.4",
    "productCategoryLevel2": "3.4",
    "categoryCount": 0
    }
    ]
    },
    {
    "productCategoryLevel2": "3.5",
    "productCategoryLevel2Description": "Product Category 3.5",
    "productCategoryLevel1": "3",
    "children": [
    {
    "productCategoryLevel3": "3.5.1",
    "productCategoryLevel3Description": "Product Category 3.5.1",
    "productCategoryLevel2": "3.5",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "3.5.2",
    "productCategoryLevel3Description": "Product Category 3.5.2",
    "productCategoryLevel2": "3.5",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "3.5.3",
    "productCategoryLevel3Description": "Product Category 3.5.3",
    "productCategoryLevel2": "3.5",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "3.5.4",
    "productCategoryLevel3Description": "Product Category 3.5.4",
    "productCategoryLevel2": "3.5",
    "categoryCount": 2
    }
    ]
    },
    {
    "productCategoryLevel2": "3.6",
    "productCategoryLevel2Description": "Product Category 3.6",
    "productCategoryLevel1": "3",
    "children": [
    {
    "productCategoryLevel3": "3.6.1",
    "productCategoryLevel3Description": "Product Category 3.6.1",
    "productCategoryLevel2": "3.6",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "3.6.2",
    "productCategoryLevel3Description": "Product Category 3.6.2",
    "productCategoryLevel2": "3.6",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "3.6.3",
    "productCategoryLevel3Description": "Product Category 3.6.3",
    "productCategoryLevel2": "3.6",
    "categoryCount": 4
    },
    {
    "productCategoryLevel3": "3.6.4",
    "productCategoryLevel3Description": "Product Category 3.6.4",
    "productCategoryLevel2": "3.6",
    "categoryCount": 1
    }
    ]
    },
    {
    "productCategoryLevel2": "3.7",
    "productCategoryLevel2Description": "Product Category 3.7",
    "productCategoryLevel1": "3",
    "children": [
    {
    "productCategoryLevel3": "3.7.1",
    "productCategoryLevel3Description": "Product Category 3.7.1",
    "productCategoryLevel2": "3.7",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "3.7.2",
    "productCategoryLevel3Description": "Product Category 3.7.2",
    "productCategoryLevel2": "3.7",
    "categoryCount": 0
    },
    {
    "productCategoryLevel3": "3.7.3",
    "productCategoryLevel3Description": "Product Category 3.7.3",
    "productCategoryLevel2": "3.7",
    "categoryCount": 2
    },
    {
    "productCategoryLevel3": "3.7.4",
    "productCategoryLevel3Description": "Product Category 3.7.4",
    "productCategoryLevel2": "3.7",
    "categoryCount": 3
    }
    ]
    },
    {
    "productCategoryLevel2": "3.8",
    "productCategoryLevel2Description": "Product Category 3.8",
    "productCategoryLevel1": "3",
    "children": [
    {
    "productCategoryLevel3": "3.8.1",
    "productCategoryLevel3Description": "Product Category 3.8.1",
    "productCategoryLevel2": "3.8",
    "categoryCount": 3
    },
    {
    "productCategoryLevel3": "3.8.2",
    "productCategoryLevel3Description": "Product Category 3.8.2",
    "productCategoryLevel2": "3.8",
    "categoryCount": 1
    },
    {
    "productCategoryLevel3": "3.8.3",
    "productCategoryLevel3Description": "Product Category 3.8.3",
    "productCategoryLevel2": "3.8",
    "categoryCount": 4
    },
    {
    "productCategoryLevel3": "3.8.4",
    "productCategoryLevel3Description": "Product Category 3.8.4",
    "productCategoryLevel2": "3.8",
    "categoryCount": 1
    }
    ]
    }
    ]
    }
    ]

这是我正在使用的代码。我正在使用类别。然后,因为在实际代码中,类别是一个承诺,但这不应有所作为

categories.then(h => {
        let filteredCategory = h.filter(x => x.productCategoryLevel1Description == "Cartons").filter(y => y.children.some(z => z.productCategoryLevel2Description == "Product Category 1.1"))
        console.log(filteredCategory)
    })

3 个答案:

答案 0 :(得分:4)

您可以存储用于访问正确属性的级别。

如果需要级别信息,则可以添加带有级别值的属性。使用更改键会产生大量开销,因为属性名称未知或需要其他变量。

.as-console-wrapper { max-height: 100% !important; top: 0; }
{{1}}

答案 1 :(得分:2)

要救援的对象(和递归)。...

class Category {
  constructor(level, description) {
    this.level = level
    this.description = description
    this.children = []
  }
  addChildren(categories) {
    this.children = this.children.concat(categories)
  }
  hasDescendantWithDescription(string) {
    if (this.description === string) return true
    return this.children.some(child => child.hasDescendantWithDescription(string))
  }
}

// exercise left for the reader:
// build a better constructor, and map the input into Category objects
// for now, in code....
const parentA = new Category(1, "TopLevelA")
const parentB = new Category(1, "TopLevelB")
const categories = [parentA, parentB]

parentA.addChildren([
  new Category(2, "A Child A"),
  new Category(2, "A Child B")
])

parentB.addChildren([
  new Category(2, "B Child A"),
  new Category(2, "B Child B")
])

// punch line is here:
console.log(categories.filter(cat => cat.hasDescendantWithDescription('B Child B')))

答案 2 :(得分:1)

您的问题源于命名。

与其给x,y和z命名,不如给它们起真正描述它们的名称。完成此操作后,Tao会变得清晰起来,您的问题将自行解决。

请参见this article

我将您的代码放入代码段中进行了修复,但是缺乏可理解的名称使意图不清楚。如果您清楚自己在做什么,请弄清楚名字,以便其他人也可以做到。

如果您无法做到这一点,则您不清楚自己在做什么,这就是问题所在。

该程序的逻辑尚不清楚,因为您进入了未命名的领域。 x是未知数。解决x

其余的将紧随其后。