英文:
How to filter an array of object by multiple property values?
问题
如果我的数组包含一个值,该值是一个数组(多个标签),如何筛选数组,以使其同时包含这两个标签。从此示例出发,以下是我目前的代码。
let articles = [
{ title: '标题 1', tags: ['JavaScript', 'ES6'], category: 'JavaScript' },
{ title: '标题 2', tags: ['React', 'TypeScript'], category: 'React' },
{ title: '标题 3', tags: ['JavaScript', 'Inheritance', 'Prototype'], category: 'JavaScript' }
];
let search = 'ES6';
let search2 = 'JavaScript';
let result = articles.filter((data) => data.tags.includes(search) && data.tags.includes(search2));
console.log(result);
但它似乎只是筛选为“任一”,而不是“都”。在这个示例中,我希望它只返回标题 1。
谢谢
英文:
If my array contains a value which is an array (multiple tags) how can I filter the array so it has to contain both tags. Working from this example this is the code I have so far.
let articles = [{title: 'title 1', tags :["JavaScript", "ES6"], category: "JavaScript"},{title: 'title 2', tags :["React", "TypeScript"], category: "React"},{title: 'title 3', tags :["JavaScript", "Inheritance", "Prototype"], category: "JavaScript"}]
let search = "ES6";
let search2 = "JavaScript";
let result = articles.filter(((data)=>data.tags.includes(search && search2)))
console.log(result)
But it seems to only filter as "either" rather than "both". In this example I would like it to return title 1 only.
Thanks
答案1
得分: 0
你可以为所需的字符串创建一个数组,并使用短路方式检查所有字符串。
result = articles.filter(({ tags }) => [search, search2].some(s => tags.includes(s)));
英文:
You could take an array for the wanted strings and check against all with a short circuit.
result = articles.filter(({ tags }) => [search, search2].some(s => tags.includes(s)));
答案2
得分: 0
let articles = [{
title: 'title 1',
tags: ["JavaScript", "ES6"],
category: "JavaScript"
}, {
title: 'title 2',
tags: ["React", "TypeScript"],
category: "React"
}, {
title: 'title 3',
tags: ["JavaScript", "Inheritance", "Prototype"],
category: "JavaScript"
}]
let search = "ES6";
let search2 = "JavaScript";
let result = articles.filter((data) => [search, search2].every((tag) => data.tags.includes(tag)))
console.log(result)
// Alternative with cleaner syntax
let articles = [{
title: 'title 1',
tags: ["JavaScript", "ES6"],
category: "JavaScript"
}, {
title: 'title 2',
tags: ["React", "TypeScript"],
category: "React"
}, {
title: 'title 3',
tags: ["JavaScript", "Inheritance", "Prototype"],
category: "JavaScript"
}]
let search = "ES6";
let search2 = "JavaScript";
let result = articles.filter((data) => data.tags.includes(search) && data.tags.includes(search2))
console.log(result)
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let articles = [{
title: 'title 1',
tags: ["JavaScript", "ES6"],
category: "JavaScript"
}, {
title: 'title 2',
tags: ["React", "TypeScript"],
category: "React"
}, {
title: 'title 3',
tags: ["JavaScript", "Inheritance", "Prototype"],
category: "JavaScript"
}]
let search = "ES6";
let search2 = "JavaScript";
let result = articles.filter(((data) => [search, search2].every((tag) => data.tags.includes(tag))))
console.log(result)
<!-- end snippet -->
Alternative with cleaner syntax
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let articles = [{
title: 'title 1',
tags: ["JavaScript", "ES6"],
category: "JavaScript"
}, {
title: 'title 2',
tags: ["React", "TypeScript"],
category: "React"
}, {
title: 'title 3',
tags: ["JavaScript", "Inheritance", "Prototype"],
category: "JavaScript"
}]
let search = "ES6";
let search2 = "JavaScript";
let result = articles.filter(((data) => data.tags.includes(search) && data.tags.includes(search2)))
console.log(result)
<!-- end snippet -->
答案3
得分: 0
我们可以使用集合来获取在每个标签中对唯一数组元素进行搜索的结果:
let search = "ES6";
let search2 = "JavaScript";
let articles = [{tags:["ES6", "JavaScript"]}, {tags:["ES6", "JavaQScript"]}, {tags:["JavaScript"]}, {tags:["ES6"]}]
let result = articles.filter((data)=>{const set1 = new Set(data.tags); return (set1.has(search) && set1.has(search2)) })
console.log(result)
英文:
We can use the set to get the search done on unique array elements in each tags:
let search = "ES6";
let search2 = "JavaScript";
let articles = [{tags:["ES6", "JavaScript"]}, {tags:["ES6", "JavaQScript"]}, {tags:[ "JavaScript"]}, {tags:["ES6"]}]
let result = articles.filter((data)=>{const set1 = new Set(data.tags); return (set1.has(search) && set1.has(search2)) })
console.log(result)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论