简体   繁体   English

没有使用过滤器获取对象的子代

[英]Not getting children of object with filter

I'm using a filter on an array with objects that have children.我在一个带有子对象的数组上使用过滤器。 But when i filter on the parent i only get the data i filtered on and not the children of that parent.但是当我过滤父项时,我只会得到我过滤的数据,而不是该父项的子项。 What am I doing wrong ?我究竟做错了什么 ?

app.get('/api/categories/:category', (req, res) => {
    res.send(getFilteredCategories(req.params.category));
  });


function getFilteredCategories(category){
    const categorieArray = [categories];
    const filteredCategory = categorieArray.filter(d => d.productCategoryLevel1Description = category);
    console.log(filteredCategory);
    return filteredCategory;   
}

for example when i go " http://localhost:3000/api/categories/Cartons " I'm getting例如,当我去“ http://localhost:3000/api/categories/Cartons ”我得到

[
{
"productCategoryLevel1Description": "Cartons"
}
]

but what i want is :但我想要的是:

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

Categories is a json object i'm getting from a fetch. Categories 是我从 fetch 中获取的 json 对象。 This is whats inside 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
    }
    ]
    }
    ]
    },
    {},
    {}
    ]

so what I wanna do is filter on cartons for example and get all cartons with the children of carton.所以我想做的是过滤纸箱,然后用纸箱的孩子来获取所有纸箱。

The issue might be: const filteredCategory = categorieArray.filter(d => d.productCategoryLevel1Description = category);问题可能是: const filteredCategory = categorieArray.filter(d => d.productCategoryLevel1Description = category);

You need to use == or === to check for equality (prefer === to check type as well as value)您需要使用 == 或 === 检查相等性(首选 === 检查类型和值)

also const categorieArray = [categories];还有const categorieArray = [categories];

you are creating an array of a single entry - the categories object.您正在创建一个包含单个条目的数组 - 类别对象。 If Categories is already an array you can do categories.filter .如果 Categories 已经是一个数组,你可以做categories.filter If categories not an array you need to do some work on before you can use .filter as it exists only on Array types, eg Object.values(catgories).filter()如果类别不是数组,您需要在使用.filter之前做一些工作,因为它只存在于数组类型,例如Object.values(catgories).filter()

function getFilteredCategories(category){
    //check next line
    const categorieArray = Json;
    const filteredCategory = categorieArray.filter(d => d.productCategoryLevel1Description == category);
    console.log(filteredCategory);
    return filteredCategory;   
}

This will work!这会奏效! The problem with your code is that you are copying the array , which shallow copies the array , before it is going to filter, the array is being stripped(nested elements aren't being copied), when you use filter it gives you new array so you don't need to run filter on copied array as it doesn't mutates the original array.您的代码的问题在于您正在复制数组,它浅复制数组,在过滤之前,数组被剥离(未复制嵌套元素),当您使用过滤器时,它会为您提供新数组所以你不需要在复制的数组上运行过滤器,因为它不会改变原始数组。

just run filter on original array and it will work fine只需在原始数组上运行过滤器,它就会正常工作

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

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