【发布时间】:2019-04-14 20:05:22
【问题描述】:
以下代码发出 API 请求并显示收到的所有索引,这是一个 JSON 响应。
我需要输出从服务器响应获得的单个数据数组索引,而不是呈现列表:
class Test extends React.Component {
constructor(props) {
super(props)
this.state = {
data: []
}
}
//request to api
loadData() {
fetch('https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5')
.then(response => response.json())
.then(data => {
this.setState({
data: data
})
})
.catch(err => console.error(this.props.url, err.toString()))
}
componentDidMount() {
this.loadData()
}
render() {
return (
//conclusion data
{
this.state.data.map((item) => {
return <div class = "mypanel" > currency: {
item.ccy
}
exchange: {
item.buy
} < /div>
})
}
)
React.render(document.getElementById('mypanel'));
}
}
export default Test;
【问题讨论】:
-
你能说清楚一点,'输出从api获得的单个数据数组索引'是什么意思。你的意思是数据数组的长度吗?
-
我的意思是输出某个数组索引而不是所有内容。
标签: javascript node.js reactjs