Javascript 编码问题,can';t使用API元素生成数组,Reactjs

Javascript 编码问题,can';t使用API元素生成数组,Reactjs,javascript,reactjs,frontend,web-frontend,Javascript,Reactjs,Frontend,Web Frontend,好的,这是我第一次在stackoverflow上发表文章,所以基本上我对JS、React、Web开发都是新手,我很难用API的JSON响应中的一些数据制作数组 为此,我遵循了许多类似的教程,但我似乎无法正确理解,也许我在这条路径上犯了错误 以下是一些JSON: meta: {code: 200} response: holidays: Array(34) 0: date: datetime: {year: 20

好的,这是我第一次在stackoverflow上发表文章,所以基本上我对JS、React、Web开发都是新手,我很难用API的JSON响应中的一些数据制作数组

为此,我遵循了许多类似的教程,但我似乎无法正确理解,也许我在这条路径上犯了错误

以下是一些JSON:

meta: {code: 200}
   response:
    holidays: Array(34)
           0:
            date:
             datetime: {year: 2019, month: 1, day: 1}
             iso: "2019-01-01"
__proto__: Object
description: "New Year’s Day (Anul Nou) and the following day, on January 1 and 2 respectively, are annual holidays in Romania."
locations: "All"
name: "New Year's Day"
states: "All"
type: ["National holiday"]
__proto__: Object
           1: {name: "Day after New Year's Day", description: "Both New Year’s Day (Anul Nou) and the following d…d 2 respectively, are annual holidays in Romania.", date: {…}, type: Array(1), locations: "All", …}
           2: {name: "Unification Day", description: "Unification Day celebrates the political union bet…ch is deemed as the foundation of modern Romania.", date: {…}, type: Array(1), locations: "All", …}
以下是我尝试使用componentDidMount访问API的方式:

componentDidMount() {

   const endpoint = 'https://calendarific.com/api/v2/holidays?api_key=065cc39ad5c1967ae719985ce3850f264f0215b7015d801b098df1ca9fca725b&country=RO&year=2019';
   fetch(endpoint)
   .then(results => results.json())
 }
这就是我试图创建一个函数的方法,它获取数组中所有元素的年、月、日,并返回另一个仅包含所有日期的数组:

 holidayDateArray(data){
   let holidayDates= data;
   let holidays= holidayDates.response.holidays;
   let holidaysArray= holidays.map((items, i)=>{
     return {items.date.datetime.year}+" "+{items.date.datetime.month}+" "+{items.date.datetime.day}
   });
 }
基本上,我希望holidayDateArray函数返回一个由holidays数组中所有元素的日期组成的数组。

试试这个

   fetch(endpoint)
   .then(results => results.json()).then(r => console.log($.map(r.response.holidays, h => h.date)))
json()将返回一个承诺,因为当反序列化完成时,您也必须等待它。之后,您可以使用$.map迭代数组中的所有元素,并基于属性
date

返回一个数组

   fetch(endpoint)
   .then(results => results.json()).then(r => console.log($.map(r.response.holidays, h => h.date)))
json()将返回一个承诺,因为当反序列化完成时,您也必须等待它。之后,您可以使用$.map迭代数组中的所有元素,并基于属性
date

import-React{
组成部分
}从“反应”;
类MyComponent扩展组件{
componentDidMount(){
常数端点https://calendarific.com/api/v2/holidays?api_key=065cc39ad5c1967ae719985ce3850f264f0215b7015d801b098df1ca9fca725b&country=RO&year=2019';
获取(端点)
.then(response=>response.json())
。然后(myJson=>{
const listOfDates=this.getListOfDates(myJson);
//将listOfDates设置为状态变量/实例变量,并根据需要使用
控制台日志(listOfDates);
});
}
getListOfDates(myJson){
const holidayLists=myJson.response.holidays;
const dateList=holidayLists.map(holidayListItem=>{
const dateTime=holidayListItem.date.dateTime;
//return dateTime.iso;
返回`${dateTime.year}${dateTime.month}${dateTime.day}`;
});
返回日期表;
}
}

import-React{
组成部分
}从“反应”;
类MyComponent扩展组件{
componentDidMount(){
常数端点https://calendarific.com/api/v2/holidays?api_key=065cc39ad5c1967ae719985ce3850f264f0215b7015d801b098df1ca9fca725b&country=RO&year=2019';
获取(端点)
.then(response=>response.json())
。然后(myJson=>{
const listOfDates=this.getListOfDates(myJson);
//将listOfDates设置为状态变量/实例变量,并根据需要使用
控制台日志(listOfDates);
});
}
getListOfDates(myJson){
const holidayLists=myJson.response.holidays;
const dateList=holidayLists.map(holidayListItem=>{
const dateTime=holidayListItem.date.dateTime;
//return dateTime.iso;
返回`${dateTime.year}${dateTime.month}${dateTime.day}`;
});
返回日期表;
}
}

欢迎使用stackoverflow,尝试制作一个可复制的示例,说明您的Json格式不正确。在尝试任何事情之前,我会先解决这个问题欢迎使用stackoverflow,尝试制作一个可复制的示例,说明您的Json格式不正确。我会在尝试任何事情之前解决这个问题