javascript - 如何引用此 JSON 对象数据?
问题描述
我想将我的Strapi
数据库引用到我的 Next.JS 前端。如果 JSON 数据也是对象中的对象,我如何引用它?
例如,要访问名称,我使用{activity.name}
. 我怎样才能访问providerName
?
我试过{activity.provider.providerName}
了,但它不工作。我究竟做错了什么?
[
{
"id": 1,
"name": "Bowling in Mundsburg",
"activityType": "Bowling",
"profileType": "Basic",
"adventureType": "Activity",
"slug": "bowling-in-new-york",
"provider": {
"id": 1,
"providerName": "Bowlingcenter New York",
"slug": "bowlingcenter-new-york",
"published_at": "2021-09-26T12:06:12.670Z",
"created_at": "2021-09-26T12:04:28.585Z",
"updated_at": "2021-09-26T12:06:12.690Z"
},
...
]
解决方案
你可以这样试试吗?
const strapiRes = [
{
"id": 1,
"name": "Bowling in Mundsburg",
"activityType": "Bowling",
"profileType": "Basic",
"adventureType": "Activity",
"slug": "bowling-in-new-york",
"provider": {
"id": 1,
"providerName": "Bowlingcenter New York",
"slug": "bowlingcenter-new-york",
"published_at": "2021-09-26T12:06:12.670Z",
"created_at": "2021-09-26T12:04:28.585Z",
"updated_at": "2021-09-26T12:06:12.690Z"
}
}
];
const whatYouAreAskingFor = strapiRes[0].provider.providerName
另外,如果您正在循环尝试,请尝试以下操作:
strapiRes.map(activity => activity.provider.providerName)
推荐阅读
- reactjs - 如何使用 MomentJS 将自定义日期设置为 react-datepicker 的 DatePicker?
- javascript - Immutable.fromJS() 没有将我的 JSON 深度转换为不可变对象
- android - 显示由 HttpsURLConnection 发送的实际原始数据 + HTTPS 标头
- r - 循环遍历列并将字段自动拆分到新列
- react-native - 在 react-native-video 中使用 seek 的意外行为
- android - 缩放图像在某些设备中不起作用
- java - JSONObject 无法转换为 JSONArray , jsonObject.getJSONArray(KEY_DATA) 不起作用
- javascript - 在数组中更改名称 JSON
- python - 基于正则表达式 pandas 更新字段格式
- python - Keras Functional Api, InvalidArgumentError: input 1 should contain 3 elements, but got 2