js Gets the implementation code for the actual length of objects and arrays and the actual number of elements

  • 2021-06-28 10:04:14
  • OfStack

js Gets the implementation code for the actual length of objects and arrays and the actual number of elements


/* Get object, length of array, number of elements 
   *@param obj  To calculate the length of an element, you can object , array , string
  */
  function count(obj){
    var objType = typeof obj;
    if(objType == "string"){
      return obj.length;
    }else if(objType == "object"){
      var objLen = 0;
      for(var i in obj){
        objLen++;
      }
      return objLen;
    }
    return false;
  }

Related articles: