首页 > 解决方案 > 过滤嵌套对象

问题描述

我正在尝试过滤类别,我想要产品类别 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 因为在真正的代码中,类别是一个承诺,但这不应该有所作为

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)
    })

标签: javascriptarraysnode.js

解决方案


您可以存储用于寻址正确属性的级别。

如果您需要级别信息,可以添加具有级别值的属性。使用更改键会产生大量开销,因为属性名称要么未知,要么需要额外的变量。

function findChildren(array, category, level = 1) {
    var temp;
    array.some(o => {
        if (o['productCategoryLevel' + level] === category) return temp = o.children;
        return temp = findChildren(o.children || [], category, level + 1);
    });
    return temp;
}

let categories = [{ productCategoryLevel1: "1", productCategoryLevel1Description: "Cartons", sortingKeyLevel1: 1, children: [{ productCategoryLevel2: "1.1", productCategoryLevel2Description: "Product Category 1.1", children: [{ productCategoryLevel3: "1.1.1", productCategoryLevel3Description: "Product Category 1.1.1", categoryCount: 3 }, { productCategoryLevel3: "1.1.2", productCategoryLevel3Description: "Product Category 1.1.2", categoryCount: 1 }, { productCategoryLevel3: "1.1.3", productCategoryLevel3Description: "Product Category 1.1.3", categoryCount: 2 }, { productCategoryLevel3: "1.1.4", productCategoryLevel3Description: "Product Category 1.1.4", categoryCount: 2 }] }, { productCategoryLevel2: "1.2", productCategoryLevel2Description: "Product Category 1.2", children: [{ productCategoryLevel3: "1.2.1", productCategoryLevel3Description: "Product Category 1.2.1", categoryCount: 2 }, { productCategoryLevel3: "1.2.2", productCategoryLevel3Description: "Product Category 1.2.2", categoryCount: 0 }, { productCategoryLevel3: "1.2.3", productCategoryLevel3Description: "Product Category 1.2.3", categoryCount: 1 }, { productCategoryLevel3: "1.2.4", productCategoryLevel3Description: "Product Category 1.2.4", categoryCount: 3 }] }] }];

console.log(findChildren(categories, '1.1'));
.as-console-wrapper { max-height: 100% !important; top: 0; }


推荐阅读