英文:
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: "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);
<!-- 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 => post.tags.includes("funny") && post.tags.includes("good") );
答案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: "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)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论