在JavaScript中过滤包含特定值的对象数组。

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

Filter array on object's array contains javascript

问题

Here's the translated code portion:

我有一个类似这样的数组

let posts = [
  {title: "1", tags: ["funny", "good", "nice"]},
  {title: "2", tags: ["weird", "bad"]},
  {title: "3", tags: ["funny", "good"]},
];

我想要数组中只包含具有 "tags" 数组包含 "funny"  "good" 的对象数组应该包括所有 "tags" 数组同时包含 "funny"  "good" 的对象而不是仅包含 "funny"  "good" 的对象

这是我现在有的代码但它不起作用

let postsfilter = posts.filter(post => post.tags.indexOf(["funny", "good"]));

If you have any further questions or need assistance with the code, please let me know.

英文:

I have a array like this:

let posts = [
  {title: "1", tags: ["funny", "good", "nice"]},
  {title: "2", tags: ["weird", "bad"]},
  {title: "3", tags: ["funny", "good"]},
];

And I want the array to have only objects which "tags" array has "funny" and "good". The Array should include all objects which "tags" array contains funny and good. Not objects which only contain "funny" and "good".

This is what I have now but it does not work:

let postsfilter = posts.filter(post => post.tags.indexOf(["funny", "good"]));

答案1

得分: 3

你可以使用 .filter().every() 以及 .includes() 来获得所需的输出:

let posts = [
  {title: "1", tags: ["funny", "good", "nice"]},
  {title: "2", tags: ["weird", "bad"]},
  {title: "3", tags: ["funny", "good"]},
];
let filterBy = ["funny", "good"];

let output = posts.filter(
  ({ tags }) => filterBy.every(s => tags.includes(s))
);

console.log(output);
英文:

You can use .filter() along-with .every() and .includes() to get the desired output:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let posts = [
  {title: &quot;1&quot;, tags: [&quot;funny&quot;, &quot;good&quot;, &quot;nice&quot;]},
  {title: &quot;2&quot;, tags: [&quot;weird&quot;, &quot;bad&quot;]},
  {title: &quot;3&quot;, tags: [&quot;funny&quot;, &quot;good&quot;]},
];
let filterBy = [&quot;funny&quot;, &quot;good&quot;];

let output = posts.filter(
  ({ tags }) =&gt; filterBy.every(s =&gt; tags.includes(s))
);

console.log(output);

<!-- language: lang-css -->

.as-console-wrapper { max-height: 100% !important; top: 0; }

<!-- end snippet -->

答案2

得分: 2

以下是已翻译的代码部分:

你可以使用 Array#every 获取 wanted 数组的 所有 项,并使用 Array#includes 检查数组是否包含一个字符串。

let posts = [{ title: "1", tags: ["funny", "good", "nice"] }, { title: "2", tags: ["weird", "bad"] }, { title: "3", tags: ["funny", "good"] }],
    wanted = ["funny", "good"],
    result = posts.filter(({ tags }) => wanted.every(s => tags.includes(s)));

console.log(result);
英文:

You could use Array#every for getting all items of the wanted array and use Array#includes for checking if the array contains a string.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let posts = [{ title: "1", tags: ["funny", "good", "nice"] }, { title: "2", tags: ["weird", "bad"] }, { title: "3", tags: ["funny", "good"] }],
wanted = ["funny", "good"],
result = posts.filter(({ tags }) => wanted.every(s => tags.includes(s)));

console.log(result);

<!-- end snippet -->

答案3

得分: 2

let postsfilter = posts.filter(post => post.tags.includes("funny") && post.tags.includes("good"));

英文:
let postsfilter = posts.filter(post =&gt; post.tags.includes(&quot;funny&quot;) &amp;&amp; post.tags.includes(&quot;good&quot;) );

答案4

得分: 1

let posts = [
  {title: "1", tags: ["funny", "good", "nice"]},
  {title: "2", tags: ["weird", "bad"]},
  {title: "3", tags: ["funny", "good"]},
];

let filtered = posts.filter((v) => v.tags.includes("funny") && v.tags.includes("good"))

console.log(filtered)
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let posts = [
  {title: &quot;1&quot;, tags: [&quot;funny&quot;, &quot;good&quot;, &quot;nice&quot;]},
  {title: &quot;2&quot;, tags: [&quot;weird&quot;, &quot;bad&quot;]},
  {title: &quot;3&quot;, tags: [&quot;funny&quot;, &quot;good&quot;]},
];

let filtered = posts.filter((v) =&gt; v.tags.includes(&quot;funny&quot;) &amp;&amp; v.tags.includes(&quot;good&quot;))

console.log(filtered)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 23:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614522.html
匿名

发表评论

匿名网友

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

确定