3 回答

TA贡献1818条经验 获得超8个赞
首先,该 API 返回一个对象数组。您可以使用 map 方法迭代数组,例如:
const dates = verify.map(item => item.date);
另外,重要的是要提到获取是一个承诺。如果要在函数外部访问其结果,则必须对其进行处理。getHolidays
function getHolidays() {
return fetch('https://api.calendario.com.br/?json=true&ano=2020&ibge=3550308&token=bHVjYXNsdm81M0BnbWFpbC5jb20maGFzaD03ODE3NDM2MA')
.then(function (response) {
return response.json()
})
.then(function (verify) {
return verify.map(item => item.date)
})
}
getHolidays().then(function (dates) {
console.log(dates);
});

TA贡献1898条经验 获得超8个赞
如前所述@EliasSoares,您正在尝试访问数组上的对象属性。您需要索引到某个对象中才能获取该属性(该属性是 )。date
像这样的东西应该会有所帮助:
function getHolidays() {
fetch(
"https://api.calendario.com.br/?json=true&ano=2020&ibge=3550308&token=bHVjYXNsdm81M0BnbWFpbC5jb20maGFzaD03ODE3NDM2MA"
)
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log({ holidays: data });
document.getElementById("results").innerHTML = `<h1>Results:</h1>${JSON.stringify(data, null, 2)}`;
});
}
getHolidays()
Open your console
<pre id="results" />
添加回答
举报