从JSON端点保存数据
我正在尝试映射返回的json并将ID保存到配置文件中。但是,它似乎没有正确映射到数据上,id:
${ profile.id }
该位需要更改吗?任何帮助深表感谢。
他们的在线工具可以帮助我吗?
API请求:
// Grabs company data from the json url
private getProfiles() {
let config = {
headers: {'Authorization':'Bearer AQVVEqNXTWVYPpPYivKNWVO8jsTx2eveV3kBg'}
}
axios
.get("/" + "", config)
.then(response =>
response.data.map(profile => ({
id: `${ profile.id }`
}))
)
.then(profiles => {
this.setState({
profiles
});
})
// We can still use the `.catch()` method since axios is promise-based
.catch(error => this.setState({ error, isLoading: false }));
}
Json返回的数据:
{
"localizedLastName": "King",
"id": "fm0B3D6y3I",
"localizedFirstName": "Benn"
}
回答如下:我不明白,你想做什么。在地图中,您具有回调函数,但是据我所知,您在其中编写了一个对象。如果您想重写当前配置文件的ID,请输入以下内容:
response.data.map(profile => ({
profile.id = `${ profile.id }`;
}))
但是如果您希望它成为变量,则此:
response.data.map(profile => ({
let id = `${ profile.id }`;
}))