简体   繁体   English

为什么在 console.log 之后没有调用 Javascript

[英]Why Javascript is not getting called after console.log

I am trying to create a function which console.log object or an array type我正在尝试创建一个 console.log 对象或数组类型的函数

function whichDataStructure (ITEM){

     if (typeof  ITEM ==='object'){
        console.log ('I am object');
   } if (typeof  ITEM === 'array') {
    console.log ('i am array');

   } else {
    console.log(' neither');


  }
};

In Javascript Arrays are actually a kind of Object.在 Javascript 中, 数组实际上是一种对象。

You have to use the Array.isArray() function to find out if a value is an Array:你必须使用Array.isArray()函数来确定一个值是否是一个数组:

function whichDataStructure(item) {
    if (Array.isArray(item)) {
        console.log('I am an Array');
    } else if (typeof item === 'object'){
        console.log('I am an Object');
    } else {
        console.log('I am of type: ' + typeof item);
    }
};

It is important that you test if the value is an Array before testing if it is an Object.测试它是否是一个对象之前,先测试它是否是一个数组这一点很重要。 Otherwise it will always be seen as an Object.否则它将始终被视为一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM