【问题标题】:pass values of json object in react native to another object将json对象的值传递给另一个对象
【发布时间】: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>

这样做的方法可能很简单,但我对这种方法很陌生,我找不到简单的方法。

【问题讨论】:

  • 返回的数据只有参数dayend_timestart_time,那么当它们缺少titlelocationextra_description?
  • @yudhiesh 感谢您的回复,其余的将在稍后添加,只是我将其中的一部分作为示例发布
  • 在你这样做之前帮不上忙
  • endTime 中的两个数字从何而来?
  • @yudhiesh 抱歉这个错误我再次更新了问题

标签: json react-native object fetch-api


【解决方案1】:

您可以映射数据,然后将它们添加到数组events_data

函数addData将从获取请求中接收到的数据以所需的格式推送到events_data

function getvals() {
  fetch("http://*********/users/timetable")
    .then((response) => response.json())
    .then((output) => {
      addData(output, events_data);
    })
    .catch((error) => console.log(error));
}

function addData(data, data2) {
  data.forEach(d) => {
    data2.push({
      title: d.title,
      startTime: genTimeBlock(d.day, d.startTime),
      endTime: genTimeBlock(d.day, d.endTime),
      location: d.location,
      extra_description: [d.extra_description],
    });
  });
}

编辑:

如果你想渲染数据,那么你可以这样做:

const RenderData = () => {
  return (
    <View>
      {events_data &&
        events_data.result.map((data) => {
          return <Text>{data.title}</Text>;
        })}
    </View>
  );
};

【讨论】:

  • 这非常有帮助,我真的很感激,如果我想在 中调用 event_data,我有一个简单的问题,该怎么做。对不起,这听起来很奇怪,但我仍在学习。谢谢
  • 感谢您的回复,我已经投了赞成票,而且我已经更新了我的问题,events_data 对象将在哪里通过,因为我尝试了您提到的方式但不起作用。提前感谢
  • 您不应该经常更改问题并不同意我的回答,而是创建一个新问题,我也会回答。您还需要添加有关您打算添加到TimeTableView 的更多信息。
  • 很抱歉,我很抱歉。我已经批准了这个问题,并根据您的要求创建了另一个包含更多详细信息的问题:stackoverflow.com/questions/65747282/…
猜你喜欢
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
  • 2018-09-05
  • 2021-12-07
  • 1970-01-01
相关资源
最近更新 更多