Javascript determine data type

Original link: https://chegva.com/5455.html

In recent work, it is necessary to judge the type of data obtained in order to perform corresponding operations. After learning it online, I have developed a method for convenience, as follows:

 /** * @name: $determineType * @cname: Determine the type* @desc: Determine the Javascript data type* @param (*) datatype pass in the data to be determined* @panme: datatype: undefined, null, boolean, number, string, object... * @result: true or false*/export function $determineType() { return { isArray: (t) => Object.prototype.toString.call(t) === "[object Array]" , isNumber: (t) => Object.prototype.toString.call(t) === "[object Number]", isString: (t) => Object.prototype.toString.call(t) === "[ object String]", isBoolean: (t) => Object.prototype.toString.call(t) === "[object Boolean]", isObj: (t) => Object.prototype.toString.call(t) = == "[object Object]", isNull: (t) => Object.prototype.toString.call(t) === "[object Null]", isUndefined: (t) => Object.prototype.toString.call (t) === "[object Undefined]", isFunction: (t) => Object.prototype.toString.call(t) === "[object Function]", isDate: (t) => Object.prototype .toString.call(t) === "[object Date]", isMoment: (t) => Object. prototype.toString.call(t) === "[object Object]" && t._isAMomentObject, isDocument: (t) => Object.prototype.toString.call(t) === "[object Document]" || Object.prototype.toString.call(t) == "[object HTMLDocument]", isRegExp: (t) => Object.prototype.toString.call(t) === "[object RegExp]", isSymbol: (t ) => Object.prototype.toString.call(t) === "[object Symbol]", }}// Method usage example // Import method import { $determineType } from "~mixins/useUtils"// 1. Determine whether the incoming data is an array type if ($determineType().isArray(proj.owner)) { for (let i in proj.owner) { owners.push(this.addProjectData[1].options[proj.owner[ i]].label) } owners = owners.toString()} else { owners = proj.owner}// 2. Determine whether the incoming data is a number, and then convert accordingly transProjLevel(val) { return $determineType(). isNumber(val) ? PROJECT_LEVEL.find(r => r.key === val)?.name : PROJECT_LEVEL.find(r => r.name === val)?.key},

refer to:

This article is reprinted from: https://chegva.com/5455.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment