【问题标题】:Why NodeJS says that an Array is an Object? [duplicate]为什么 NodeJS 说数组是对象? [复制]
【发布时间】:2019-07-10 22:54:30
【问题描述】:

我的代码有什么问题?

let x = ['hello',1]
let y = [2]
let z = {x:"hello",y:2}
console.log("type of x:",typeof x) // expected array!
console.log("type of y:",typeof y) // expected array!
console.log("type of z:",typeof z) // expected object, ok

结果:

type of x: object
type of y: object
type of z: object

【问题讨论】:

  • 哎呀,抱歉造成污染!我在考虑 JSON 原语 而不是 Javascript 原语。

标签: node.js


【解决方案1】:

因为 JavaScript 数组是对象。

您可以使用Array.isArray() 函数检查值是否为数组。

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 2018-04-11
    • 2012-07-04
    • 2018-12-16
    • 2014-10-09
    • 1970-01-01
    相关资源
    最近更新 更多