gpt4 book ai didi

javascript - .filter 中的 ES6 .filter

转载 作者:行者123 更新时间:2023-11-29 16:36:19 28 4
gpt4 key购买 nike

所以我有如下数据:

[
{
"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 tagMoodboards = _moodboards.filter(mb => { return mb.tags.filter(t => t.value == name) })

最佳答案

您不希望在 filter 中使用 filter - 相反,在过滤器内部,检查是否有 some 标签 对象具有您想要的 .value 属性

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

关于javascript - .filter 中的 ES6 .filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51128388/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com