第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何從匹配特定鍵的 JSON 對(duì)象創(chuàng)建數(shù)組?

如何從匹配特定鍵的 JSON 對(duì)象創(chuàng)建數(shù)組?

小唯快跑啊 2023-06-09 10:47:20
我正在使用themealdb.comAPI,它為您提供了如下成分:"strIngredient1": "Quinoa","strIngredient2": "Butter","strIngredient3": "Red Chilli","strIngredient4": "Garlic","strIngredient5": "Chicken Breast","strIngredient6": "Olive Oil","strIngredient7": "Black Olives","strIngredient8": "Red Onions","strIngredient9": "Feta","strIngredient10": "Mint","strIngredient11": "Lemon","strIngredient12": "","strIngredient13": "","strIngredient14": "","strIngredient15": "","strIngredient16": "","strIngredient17": "","strIngredient18": "","strIngredient19": "","strIngredient20": "",我正在從 API 獲取數(shù)據(jù),然后使用 React Hooks 設(shè)置一個(gè)具有所有配方屬性的對(duì)象。對(duì)于成分,我想要這樣的東西:[Quinoa, Butter, Red Chili ...]。如何遍歷 JSON 并找到匹配"strIngredient"& 而不是空字符串的鍵,然后將它們添加到數(shù)組中?謝謝你!
查看完整描述

2 回答

?
長(zhǎng)風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

假設(shè)您的數(shù)據(jù)存儲(chǔ)在一個(gè)名為的變量中,mydata那么您可以這樣做:


let newArray = [];


for (const key of Object.keys(mydata)) {

    if (key.includes('strIngredient') && mydata[key] !== "") {             

        newArray.push(mydata[key]) 

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-06-09
?
守著一只汪

TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊

const getIngredients = (strIngredients) => {

    let ingredients = [];


    Object.entries(strIngredients).map(i => {

        const [key, value] = i;


        if (key.includes('strIngredient') && value) {

            ingredients.push(value)

        } else return

    })


    return ingredients

}


查看完整回答
反對(duì) 回復(fù) 2023-06-09
  • 2 回答
  • 0 關(guān)注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)