5 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊
不需要知道長(zhǎng)度,你可以使用forEach
posts.forEach(post => {
if(post.name.includes("politics")){
//Doing Stuff
}
});

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以使用方法 some來(lái)檢查其中一篇帖子是否具有該值
var name = "politics";
posts.some(singlePost => {
? ? ?return singlePost.name == name;
});

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
if(posts.filter(post => post.name.includes("politics")).length > 0){
//Doing Stuff
console.log("One of posts has a name including politics")
}

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊
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 數(shù)組。
添加回答
舉報(bào)