【发布时间】:2020-04-20 02:41:53
【问题描述】:
我一直在玩 json 对象和使用 ReactJS。由变量“res”表示的 json 对象之一。如何将“res”转换为“b”。我还从 api 获取我的 json 对象“res”。
我已通过此链接解决了我的问题:https://codesandbox.io/s/epic-leaf-sznhx?file=/src/App.js
const [res, setResult] = useState();
useEffect(() => {
(async () => {
axios.get("https://corona.lmao.ninja/v2/countries").then(response => {
setResult(response.data);
});
})();
}, []);
如何转换:
const res =
{
"0": {
"id": "1",
"countryInfo": [
{
"_id": "4",
"lat": "33"
}
]
},
"1": {
"id": "2",
"countryInfo": [
{
"_id": "8",
"lat": "41"
}
]
}
}
进入这个 json 对象:
const b =
{
"popup": {
"countryInfo": [
{
"_id": "1",
"lat": "33"
},
{
"_id": "2",
"lat": "41"
}
]
}
}
【问题讨论】:
-
您只需要更新上一个问题中的密钥,因此您将拥有
countryInfo,而不是countryInfo,请查看下面的答案并让我知道它是否适合您. -
@LuísRamalho 我收到错误:无法将未定义转换为对象
标签: javascript json reactjs nested