json 数据是数组和对象的混合体。该对象易于访问。如何访问数组中的每个数据?我想我可以使用地图,但我得到一个错误。不知道怎么用map方法传值。请帮忙。
<Query query={USER_QUERY}>
{
({loading, error, data }) => {
if (error) return 'error';
if (loading) return 'loading';
return (data.getUsers.history.dateInformation.map(data => (
<Fragment>
<p>User adid: </p>
{data.monthly.month}
</Fragment>)
));
}
}
</Query>
这是gql。
export const USER_QUERY = gql`
query{
getUsers{
gender
age
job
location {
dailyMovingAverage
home
mostStayingArea
office
}
history {
dateInformation {
monthly {
month
favorateTransitionService
movingCarRate
}
daily {
date
movingCarRate
}
}
}
}
}
这是json数据。
{
"data": {
"getUsers": [
{
"gender": "F",
"age": "26",
"job": "student",
"location": {
"dailyMovingAverage": 3,
"home": "aaa",
"mostStayingArea": "aaaa",
"office": "aaa"
},
"history": {
"dateInformation": [
{
"monthly": [
{
"month": "March",
"favorateTransitionService": "Car",
"movingCarRate": 0,
...