【发布时间】:2020-08-02 10:18:48
【问题描述】:
我一直在玩 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 a =
{
"menu_1": {
"id": "1",
"menuitem": [{
"value": "0",
"onclick": "0()"
}, {
"value": "0",
"onclick": "0()"
}]
},
"menu_2": {
"id": "2",
"menuitem": [{
"value": "2",
"onclick": "2()"
}]
}
}
进入这个 json 对象:
const b =
{
"popup": {
"menuitem": [
{
"value": "0",
"onclick": "0()"
},
{
"value": "0",
"onclick": "0()"
},
{
"value": "2",
"onclick": "2()"
}
]
}
}
【问题讨论】:
标签: javascript json reactjs nested