亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢查json數組javascript中是否存在值

如何檢查json數組javascript中是否存在值

偶然的你 2023-05-11 16:19:48
我有以下結構:_id: 'blog',    posts: [      {        _id: 'politics and economy',        name: 'politics and economy',        author: 'Mark',      },      {        _id: 'random',        name: 'random',        author: 'Michael'      }    ]我的 if 語句:if(posts.name.includes("politics"){//Doing Stuff}我怎樣才能讓它運行?我不知道數組的長度。
查看完整描述

5 回答

?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

不需要知道長度,你可以使用forEach


posts.forEach(post => {

    if(post.name.includes("politics")){

        //Doing Stuff

    }

});


查看完整回答
反對 回復 2023-05-11
?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

您可以使用方法 some來檢查其中一篇帖子是否具有該值

var name = "politics";

posts.some(singlePost => {

? ? ?return singlePost.name == name;

});


查看完整回答
反對 回復 2023-05-11
?
慕俠2389804

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")

}

 


查看完整回答
反對 回復 2023-05-11
?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

使用數組過濾方法。關聯


查看完整回答
反對 回復 2023-05-11
?
RISEBY

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 數組。



查看完整回答
反對 回復 2023-05-11
  • 5 回答
  • 0 關注
  • 243 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號