【发布时间】:2023-03-24 23:16:02
【问题描述】:
我使用 fetch API 从后端服务器获取数据。我从 fetch 函数返回 JSON 对象数据,我想将对象中的每个值传递给另一个对象。
function getvals() {
return fetch('http://*********/users/timetable')
.then(response => response.json())
.then((responseData) => {
console.log(responseData);
return responseData;
})
.catch(error => console.log(error))
}
函数getvals()正在返回对象数据,这些数据如下所示:
[{"title": "Math","day": "FRI", "end_time": 11, "start_time": 9,location: "Classroom 403",extra_descriptions: "Chen"}]
现在我希望将对象数据的每一个值都传递给这个对象:
const events_data = [
{
title: "Math",
startTime: genTimeBlock("WED", 9),
endTime: genTimeBlock("WED", 10),
location: "Classroom 403",
extra_descriptions: ["Kim", "Lee"],
},
{
title: "Mandarin",
startTime: genTimeBlock("TUE", 9),
endTime: genTimeBlock("TUE", 10),
location: "Language Center",
extra_descriptions: ["Chen"],
},
{
title: "Japanese",
startTime: genTimeBlock("FRI", 9),
endTime: genTimeBlock("FRI", 10),
location: "Language Center",
extra_descriptions: ["Nakamura"],
},
];
例如 getvals() 函数中的 day 值,我想将其传递到 startTime 中的 events_data 对象中
然后在视图中events_data 将被传递给events props:
<SafeAreaView style={{ flex: 1, padding: 30 }}>
<View style={styles.container}>
<TimeTableView
scrollViewRef={this.scrollViewRef}
**events={// events.data will be passed here as object format //}**
pivotTime={8}
pivotDate={this.pivotDate}
numberOfDays={this.numOfDays}
onEventPress={getvals}
headerStyle={styles.headerStyle}
formatDateHeader="dddd"
locale="en"
/>
</View>
</SafeAreaView>
这样做的方法可能很简单,但我对这种方法很陌生,我找不到简单的方法。
【问题讨论】:
-
返回的数据只有参数
day、end_time和start_time,那么当它们缺少title、location和extra_description? -
@yudhiesh 感谢您的回复,其余的将在稍后添加,只是我将其中的一部分作为示例发布
-
在你这样做之前帮不上忙
-
endTime中的两个数字从何而来? -
@yudhiesh 抱歉这个错误我再次更新了问题
标签: json react-native object fetch-api