1
export const apiDataDay = async () => {
  try {
    var days = [
      "monday",
      "tuesday",
      "wednesday",
      "thursday",
      "friday",
      "saturday",
      "sunday",
    ];
    var date = new Date();
    var dayName = days[date.getDay()-1];
    console.log("Today's Day is", dayName);
    console.log("Today's DayNumber is", date.getDay()-1);


// I want to do the following:-
//   const { data : {dayName} } = await axios.get(`${url}/schedule/${dayName}`);
// The API's works like this
//https://api.jikan.moe/v3/schedule/tuesday
// data: {tuesday : {............}}
//https://api.jikan.moe/v3/schedule/wednesday
// data: {wednesday : {............}}
//https://api.jikan.moe/v3/schedule/saturday
// data: {saturday : {............}}


    const { data } = await axios.get(`${url}/schedule/${dayName}`);
    console.log(data);
    return data;
  } catch (error) {
    console.log(error);
  }
};

当 API 动态变化时,我想使用变量 dayName 从数组中解构数据对象,所以我也想以动态方式访问数据。

我想动态访问 API 的数据对象,{data :{dayName}},我可以根据天解构 api 数据对象,将它填充。实现这一目标的最佳方法是什么?

4

0 回答 0