仅当包含搜索数组匹配中的所有项目时才过滤 JSON
Filtering JSON only if contains all items in search array match
我可以发誓我在多个地方看到过这样的例子,但是谁能告诉我我如何只从下面的 JSON 对象中匹配 return 项匹配所有标签搜索数组 - 例如:
[
{
"ref": "1",
"title": "Title 1",
"description": "Description 1...",
"tags": ["A2", "B1", "D1", "E3", "E4"]
},
{
"ref": "2",
"brand": "Title 2",
"description": "Description 2...",
"tags": ["A1", "A2", "C1", "D1", "E1", "E2", "E3", "E4"]
},
{
"ref": "3",
"title": "Title 3",
"description": "Description 3...",
"tags": ["A5", "A6", "B1", "D1", "E3"]
},
{
"ref": "4",
"title": "Title 4",
"description": "Description 4...",
"tags": ["A5", "A6", "B1", "E1", "E3"]
}
]
我在标签数组中 match/find 的搜索数组是:
["B1", "D1"]
这将 return 过滤后 JSON 在这种情况下只有项目参考 1 和 2...
如果您使用的是 java 脚本,那么这应该有效..
const array1 = [
{
"ref": "1",
"title": "Title 1",
"description": "Description 1...",
"tags": ["A2", "B1", "D1", "E3", "E4"]
},
{
"ref": "2",
"brand": "Title 2",
"description": "Description 2...",
"tags": ["A1", "A2", "C1", "D1", "E1", "E2", "E3", "E4"]
},
{
"ref": "3",
"title": "Title 3",
"description": "Description 3...",
"tags": ["A5", "A6", "B1", "D1", "E3"]
},
{
"ref": "4",
"title": "Title 4",
"description": "Description 4...",
"tags": ["A5", "A6", "B1", "E1", "E3"]
}
]
let tags = [ "B1", "D1"]
console.log(array1.filter( f => tags.every(e => f.tags.includes(e)))
);
我可以发誓我在多个地方看到过这样的例子,但是谁能告诉我我如何只从下面的 JSON 对象中匹配 return 项匹配所有标签搜索数组 - 例如:
[
{
"ref": "1",
"title": "Title 1",
"description": "Description 1...",
"tags": ["A2", "B1", "D1", "E3", "E4"]
},
{
"ref": "2",
"brand": "Title 2",
"description": "Description 2...",
"tags": ["A1", "A2", "C1", "D1", "E1", "E2", "E3", "E4"]
},
{
"ref": "3",
"title": "Title 3",
"description": "Description 3...",
"tags": ["A5", "A6", "B1", "D1", "E3"]
},
{
"ref": "4",
"title": "Title 4",
"description": "Description 4...",
"tags": ["A5", "A6", "B1", "E1", "E3"]
}
]
我在标签数组中 match/find 的搜索数组是:
["B1", "D1"]
这将 return 过滤后 JSON 在这种情况下只有项目参考 1 和 2...
如果您使用的是 java 脚本,那么这应该有效..
const array1 = [
{
"ref": "1",
"title": "Title 1",
"description": "Description 1...",
"tags": ["A2", "B1", "D1", "E3", "E4"]
},
{
"ref": "2",
"brand": "Title 2",
"description": "Description 2...",
"tags": ["A1", "A2", "C1", "D1", "E1", "E2", "E3", "E4"]
},
{
"ref": "3",
"title": "Title 3",
"description": "Description 3...",
"tags": ["A5", "A6", "B1", "D1", "E3"]
},
{
"ref": "4",
"title": "Title 4",
"description": "Description 4...",
"tags": ["A5", "A6", "B1", "E1", "E3"]
}
]
let tags = [ "B1", "D1"]
console.log(array1.filter( f => tags.every(e => f.tags.includes(e)))
);