How to determine whether an object is a null object

You can determine whether an object is an empty object in the following two ways:

1、Object.keys() 

Get the attribute list of the object, and then determine whether the length of the list is 0

      let obj = {};
      if (Object.keys(obj).length === 0) {
        console.log("空对象");
      }

2、 for...in

Loop through the object. If there are attributes, it is not an empty object.

      const obj = {};
      let isEmpty = true;
      for (let item in obj) {
        isEmpty = false;
      }
      if (isEmpty) {
        console.log("obj is empty");
      }

Guess you like

Origin blog.csdn.net/qq_38290251/article/details/134388501