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 數組。
添加回答
舉報