如果array
的{{1}}是用户选择的value
的话,我想过滤category
中的每一个。
例如,我有value
的{{1}}
array
我想exampleData
仅将[
{
"node": {
"id": "377856cd-9477-58a4-9596-888ebc8c03d5",
"title": "Tubing & Sledding",
"category": [
"Winter"
]
}
},
{
"node": {
"id": "a7370249-af7a-538a-aaae-91f308575bb8",
"title": "Snowmobiling",
"category": [
"Summer",
"Winter"
]
}
},
{
"node": {
"id": "4136389a-77b7-5e07-8c95-1c856983d27b",
"title": "Snowshoeing",
"category": [
"Winter"
]
}
},
{
"node": {
"id": "79fc5efa-e1a4-5e65-94cd-97a5245ed0af",
"title": "X-Country Skiing",
"category": [
"Winter"
]
}
}
]
字段filter
中包含return
的{{1}} objects
数组。 / p>
为此,我已经尝试
Summer
但是,category
显示的结果为零。
我在这里做错了什么?
答案 0 :(得分:1)
您应该返回调用.includes
的布尔结果:
const exampleCategory = exampleData.filter(({ node: { category }}) => {
return category.includes('Summer');
});
此外,您可以直接执行此操作而无需返回
const exampleCategory = exampleData.filter(({ node: { category }}) => category.includes('Summer'));
示例
const exampleData = [{ "node": { "id": "377856cd-9477-58a4-9596-888ebc8c03d5", "title": "Tubing & Sledding", "category": [ "Winter" ] } }, { "node": { "id": "a7370249-af7a-538a-aaae-91f308575bb8", "title": "Snowmobiling", "category": [ "Summer", "Winter" ] } }, { "node": { "id": "4136389a-77b7-5e07-8c95-1c856983d27b", "title": "Snowshoeing", "category": [ "Winter" ] } }, { "node": { "id": "79fc5efa-e1a4-5e65-94cd-97a5245ed0af", "title": "X-Country Skiing", "category": [ "Winter" ] } }];
const exampleCategory = exampleData.filter(({ node: { category }}) => category.includes('Summer'));
console.log(exampleCategory);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
您犯了两个错误
您正在从传递给过滤器的回调中返回undefined
。您需要显式返回includes
的结果,或使用箭头函数隐式返回结果。
您使用的是错误的值includes()
。activity
将是undefined
。只需检查element.node.category.includes
const exampleData = [ { "node": { "id": "377856cd-9477-58a4-9596-888ebc8c03d5", "title": "Tubing & Sledding", "category": [ "Winter" ] } }, { "node": { "id": "a7370249-af7a-538a-aaae-91f308575bb8", "title": "Snowmobiling", "category": [ "Summer", "Winter" ] } }, { "node": { "id": "4136389a-77b7-5e07-8c95-1c856983d27b", "title": "Snowshoeing", "category": [ "Winter" ] } }, { "node": { "id": "79fc5efa-e1a4-5e65-94cd-97a5245ed0af", "title": "X-Country Skiing", "category": [ "Winter" ] } } ]
const exampleCategory = exampleData.filter(x => x.node.category.includes('Summer'))
console.log(exampleCategory)
您还可以使用对象分解。
const exampleData = [ { "node": { "id": "377856cd-9477-58a4-9596-888ebc8c03d5", "title": "Tubing & Sledding", "category": [ "Winter" ] } }, { "node": { "id": "a7370249-af7a-538a-aaae-91f308575bb8", "title": "Snowmobiling", "category": [ "Summer", "Winter" ] } }, { "node": { "id": "4136389a-77b7-5e07-8c95-1c856983d27b", "title": "Snowshoeing", "category": [ "Winter" ] } }, { "node": { "id": "79fc5efa-e1a4-5e65-94cd-97a5245ed0af", "title": "X-Country Skiing", "category": [ "Winter" ] } } ]
const exampleCategory = exampleData.filter(({node:{category}}) => category.includes('Summer'))
console.log(exampleCategory)