Js determines whether the parameters of String Array and Object are undefined or null

  • 2020-03-26 23:06:58
  • OfStack

Sometimes we run into situations where some front-end control needs to determine whether the submitted data is empty during data validation on the server side. If it's string data for a normal form, you just need to determine the length after trim, and the data you need here can be of various types (Numbers, strings, arrays, objects, and so on), serialized through json.stringify (data) and then passed.

Here, the following data values are defined as "null values" :

The & # 8226; undefined
The & # 8226; null
The & # 8226; Empty and pure blank strings: '', '      'and so on.
The & # 8226; Empty array: []
The & # 8226; Empty object: {}

Data values other than this are considered not null.

Null and undefined are easy to identify, but for other types, we need to get their data types in order to use the corresponding method to detect whether the data is empty. The easiest way to think about this is to use the typeof operator:


<SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">if(typeof data === 'number') {
  //deal with numbers
}</SPAN>

However, typeof returns only 'object', 'function', 'number', 'Boolean', 'string' and 'undefined'. Many native objects such as Date and RegExp cannot be distinguished from objects created with {}. In addition, typeof returns different values for some basic data types (String, Number, Boolean) and their corresponding basic wrapper type data, such as:

<SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">console.log(typeof false); //'boolean'
console.log(typeof new Boolean(false)); //'object'
console.log(typeof 1); //'number'
console.log(typeof new Number(1)); //'object'
console.log(typeof ''); //'string'
console.log(typeof new String('')); //'object'</SPAN>

It also has an impact on our judgment.

Use the instanceof? This only determines the object, and there is the problem that multiple similar objects do not share prototype when there are multiple frames, so objects obtained from other frames cannot be judged correctly.

Well, there is one of the most simple and most reliable method: Object. The prototype. ToString. For different types of data, this method can return '[object object]', '[object Array]', '[object String]' and so on, which is very convenient to judge. Note that in IE8 and below, this method returns '[object object]' for null, undefined, window, etc. Fortunately, this does not prevent us from using it to identify empty objects.

Let's go straight to the code and see the comments.


var isEmptyValue = function(value) {
            var type;
            if(value == null) { //Value === undefined || value === null
                return true;
            }
            type = Object.prototype.toString.call(value).slice(8, -1);
            switch(type) {
            case 'String':
                return !$.trim(value);
            case 'Array':
                return !value.length;
            case 'Object':
                return $.isEmptyObject(value); //Ordinary objects use for... In is false with key
            default:
                return false; //All other objects are treated as non-empty
            }
        };


Related articles: