.filter中的ES6 .filter [英] ES6 .filter within a .filter
本文介绍了.filter中的ES6 .filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有如下数据:
[
{
"id": 0,
"title": "happy dayys",
"owner": {"id": "1", "username": "dillonraphael"},
"tags": [{"value": "Art", "label": "Art"}],
"items": []
},
{
"id": 1,
"title": "happy dayys",
"owner": {"id": "1", "username": "dillonraphael"},
"tags": [{"value": "Architecture", "label": "Architecture"}],
"items": []
},
]
我正在尝试过滤数组,并且仅在标签数组包含 == $ c $的值时返回c>到另一个字符串。
I'm trying to filter through an array and only return if the tags array contains a value that is ==
to another string.
这是我想出的,但似乎仍然会发回整个数组:
This is what I came up with but still seems to be sending back the whole array:
const tagMoodboards = _moodboards.filter(mb => { return mb.tags.filter(t => t.value == name) })
推荐答案
您不想要过滤器
内的过滤器
-而是在过滤器内,检查 some $
标记
对象中的c $ c>具有所需的 .value
属性
You don't want a filter
inside a filter
- rather, inside the filter, check if some
of the tags
objects have the .value
property that you want
const _moodboards = [
{
"id": 0,
"title": "happy dayys",
"owner": {"id": "1", "username": "dillonraphael"},
"tags": [{"value": "Art", "label": "Art"}],
"items": []
},
{
"id": 1,
"title": "happy dayys",
"owner": {"id": "1", "username": "dillonraphael"},
"tags": [{"value": "Architecture", "label": "Architecture"}],
"items": []
},
];
const name = 'Architecture';
console.log(_moodboards.filter(({ tags }) => (
tags.some(({ value }) => value === name)
)));
这篇关于.filter中的ES6 .filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文