【问题标题】:Why do the elements in my array show up as undefined?Why do the elements in my array show up as undefined?
【发布时间】:2022-12-02 09:05:14
【问题描述】:

I can't seem to figure out why I can't get the information out of the array

const usedPlatformLog: Date[] = []
users.forEach(el => {
    usedPlatformLog.push(el.lastUsed)
})
console.log(usedPlatformLog) // shows array with indexes 0 and 1
console.log(usedPlatformLog[0]) // log response undefined

【问题讨论】:

  • "shows array with indexes 0 and 1"- What data does the first console.log show for those indexes?

标签: typescript


【解决方案1】:

undefined is a valid value, and can be pushed into arrays.

your array is [undefined, undefined] probably because users[0].lastUsed and users[1].lastUsed are also undefined. Maybe console.log(users) so you can actually see what's in that array.

【讨论】:

    猜你喜欢
    • 2018-11-07
    • 2022-12-01
    • 2022-12-01
    • 1970-01-01
    • 2022-12-02
    • 2022-12-01
    • 2022-12-02
    • 2022-12-27
    • 2022-12-02
    相关资源
    最近更新 更多