【发布时间】:2020-03-28 20:12:01
【问题描述】:
我正在尝试解析和映射嵌套对象,如下所示:
{
"country": "germany",
"timeline": {
"cases": {
"1/22/20": 0,
"1/23/20": 0,
"1/24/20": 0,
"1/25/20": 0,
"1/26/20": 0,
"1/27/20": 1,
"1/28/20": 4,
"1/29/20": 4,
"1/30/20": 4
}
}
}
这是我的第一次尝试,我尝试了几种在组件中插入响应的变体。
import React, { Component } from 'react'
class Chart extends Component {
state = {
historyStats: [],
// error: true
};
componentDidMount() {
fetch('https://corona.lmao.ninja/v2/historical/germany')
.then(res => res.json())
.then((data) => {
this.setState({ historyStats: data })
})
.catch(console.log)
}
render() {
const {historyStats} = this.state;
console.log(Object.values(historyStats));
return (
<div>
{Object.values(historyStats).map(test => (
<p>{test[0]} | {test[1]}</p>
))}
</div>
)
}
}
export default Chart;
我怎样才能实现这样的输出:
test[0]} = 1/22/20 - 日期
test[1] = 0 - 值
帮助表示赞赏 谢谢
【问题讨论】:
-
您想要的预期结果是什么?因为你只在你的回报中显示
<p> germany | Object <p>之类的东西,因为test[1]是来自键timeline的对象 -
我想输出,像这样:{test[0]} = 1/22/20 | {test[1] = 0
-
@BloodOverdrive 编辑帖子并在那里添加输出,从这里看有点不可读
标签: javascript json multidimensional-array