尽管数组具有值,但返回的长度为0 [英] array returns length of 0 although it has values
本文介绍了尽管数组具有值,但返回的长度为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我试图将项目推送到数组,尽管数组中有项目,但返回的长度似乎为0.
so i am trying to push items to an array and it appears to be return a length of 0 although there are items in the array.
let calendarDates = []
async function getDates() {
const response = await fetch('/calendars/fetch_dates')
let res = await response.json()
res.forEach(el => {
calendarDates.push(el)
})
}
getDates()
createCalendar(date, side)
.
.
.
function createCalendar(date, side) {
console.log('createCalendar', calendarDates, "is array?", Array.isArray(calendarDates), 'length', calendarDates.length)
.
.
}
我的console.log正在打印calendarDates和length: 数组
my console.log is printing calendarDates and length : array
console.log位于单独的函数中
the console.log lives in a seperate function
那么为什么长度返回0? 当尝试控制台记录一个forEach循环时,什么也没有返回,所以我不认为浏览器显示的是错误的值
so why is it returning 0 for the length? when trying to console log a forEach loop nothing returns either so i dont thing its the browser showing the wrong value for fun
推荐答案
这将有所帮助
async function Main() {
let calendarDates = []
calendarDates = await getDates();
createCalendar(date, side)
}
async function getDates() {
const response = await fetch('/calendars/fetch_dates')
return await response.json();
}
function createCalendar(date, side) {
console.log('createCalendar', calendarDates, "is array?", Array.isArray(calendarDates), 'length', calendarDates.length);
}
Main();
这篇关于尽管数组具有值,但返回的长度为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文