5 回答

TA贡献1853条经验 获得超9个赞
不需要知道长度,你可以使用forEach
posts.forEach(post => {
if(post.name.includes("politics")){
//Doing Stuff
}
});

TA贡献1783条经验 获得超4个赞
您可以使用方法 some来检查其中一篇帖子是否具有该值
var name = "politics";
posts.some(singlePost => {
return singlePost.name == name;
});

TA贡献1719条经验 获得超6个赞
if(posts.filter(post => post.name.includes("politics")).length > 0){
//Doing Stuff
console.log("One of posts has a name including politics")
}

TA贡献1856条经验 获得超5个赞
const posts = [
{
_id: 'politics and economy',
name: 'politics and economy',
author: 'Mark'
},
{
_id: 'random',
name: 'random',
author: 'Michael'
}
]
posts.forEach(post => {
if(post.name.includes("politics")){
//Do Your Stuff
console.log(post.name)
}
});
您必须遍历 posts 数组。
添加回答
举报