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

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

我想在 reactjs 中將對(duì)象轉(zhuǎn)換為整數(shù)類型

我想在 reactjs 中將對(duì)象轉(zhuǎn)換為整數(shù)類型

qq_笑_17 2022-01-13 16:33:14
我想將一個(gè)對(duì)象轉(zhuǎn)換為一個(gè)整數(shù)并將其傳遞給一個(gè) url,但在進(jìn)行轉(zhuǎn)換后它被轉(zhuǎn)換為 NAN。export const fetchById= (id) =>dispatch =>{    //id: object    const d= id.toString(); //string    const i= parseInt(d,10); //number    console.log(typeof id); //number    fetch(`https://cors-anywhere.herokuapp.com/https://api.nasa.gov/neo/rest/v1/neo?asteroid_id=${i}&api_key=demo_key`)    .then(res => res.json())        .then(data =>{            // const d= {...data.near_earth_objects};            return dispatch({                type: actions.fetchByIds, data: data            });        });
查看完整描述

1 回答

?
繁星coding

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

無需將整個(gè)對(duì)象轉(zhuǎn)換為字符串。因?yàn)樽址瘜?duì)象將返回結(jié)果JSON.stringify(),就像"{key: val}"


要實(shí)現(xiàn)當(dāng)前任務(wù),您可以直接將變量解析為整數(shù),但請(qǐng)確保您希望它為整數(shù)而不是浮點(diǎn)數(shù)。


因此,如果這樣編寫,您的代碼可能會(huì)起作用:


 export const fetchById= (id) =>dispatch =>{

     //id: object

     const i= parseInt(id); //number

     console.log(typeof id); //number

     fetch(`https://cors-anywhere.herokuapp.com/https://api.nasa.gov/neo/rest/v1/neo?asteroid_id=${i}&api_key=demo_key`)

     .then(res => res.json())

     .then(data =>{

        // const d= {...data.near_earth_objects};

        return dispatch({

            type: actions.fetchByIds, data: data

        });

    });

順便說一句,將其轉(zhuǎn)換為 int 是沒有用的,因?yàn)槟龅闹皇窃诓樵冎惺褂盟?,并且查詢將被轉(zhuǎn)換為字符串,如果您asteroid_id在服務(wù)器中記錄類型,它將是字符串。


查看完整回答
反對(duì) 回復(fù) 2022-01-13
  • 1 回答
  • 0 關(guān)注
  • 674 瀏覽
慕課專欄
更多

添加回答

舉報(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)