如何通过多个属性值筛选对象数组?

huangapple go评论83阅读模式
英文:

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: &#39;title 1&#39;,
  tags: [&quot;JavaScript&quot;, &quot;ES6&quot;],
  category: &quot;JavaScript&quot;
}, {
  title: &#39;title 2&#39;,
  tags: [&quot;React&quot;, &quot;TypeScript&quot;],
  category: &quot;React&quot;
}, {
  title: &#39;title 3&#39;,
  tags: [&quot;JavaScript&quot;, &quot;Inheritance&quot;, &quot;Prototype&quot;],
  category: &quot;JavaScript&quot;
}]

let search = &quot;ES6&quot;;
let search2 = &quot;JavaScript&quot;;

let result = articles.filter(((data) =&gt; [search, search2].every((tag) =&gt; 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: &#39;title 1&#39;,
  tags: [&quot;JavaScript&quot;, &quot;ES6&quot;],
  category: &quot;JavaScript&quot;
}, {
  title: &#39;title 2&#39;,
  tags: [&quot;React&quot;, &quot;TypeScript&quot;],
  category: &quot;React&quot;
}, {
  title: &#39;title 3&#39;,
  tags: [&quot;JavaScript&quot;, &quot;Inheritance&quot;, &quot;Prototype&quot;],
  category: &quot;JavaScript&quot;
}]

let search = &quot;ES6&quot;;
let search2 = &quot;JavaScript&quot;;

let result = articles.filter(((data) =&gt; data.tags.includes(search) &amp;&amp; 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 = &quot;ES6&quot;;
let search2 = &quot;JavaScript&quot;;
let articles = [{tags:[&quot;ES6&quot;, &quot;JavaScript&quot;]}, {tags:[&quot;ES6&quot;, &quot;JavaQScript&quot;]}, {tags:[ &quot;JavaScript&quot;]}, {tags:[&quot;ES6&quot;]}]

let result = articles.filter((data)=&gt;{const set1 = new Set(data.tags); return (set1.has(search) &amp;&amp; set1.has(search2)) })

console.log(result)

huangapple
  • 本文由 发表于 2020年1月3日 17:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定