【发布时间】:2022-07-03 21:17:47
【问题描述】:
API 新手,使用 https://swapi.dev/ API。
在https://swapi.dev/api/people 上,我正在尝试提取“homeworld”属性。它以 URL 的形式存在。
但我真的希望用行星的名称来代替 URL。
[![例如:[1]][1] [1]:https://i.stack.imgur.com/oKWOb.gif
const getPeople = async () => {
try{
//fetch data from api
const res = await axios.get('https://swapi.dev/api/people')
const pl = await axios.get('https://swapi.dev/api/planets/')
//set People
setPeople(res.data.results)
setPlanet(pl.data.results)
setLoading(true)
// console.log(res.data.results)
People.map((item)=>{console.log(item.name)})
Planet.map((x) => {console.log(x.name)});
Planet.map((y) => {console.log(y.residents)});
// console.log(pl.data.results)
} catch (err) {
alert(err.message);
}
}
如何让 Planet item.name 替换 people item.homeland URL 值?
【问题讨论】:
标签: javascript axios