javascript - check if object is empty

前端 未结 5 1028
感情败类
感情败类 2021-02-05 08:57

I am trying to create to javascript/jquery test to check if my object is empty and cannot figure it out.

Here is the object when it has something in it:

         


        
5条回答
  •  广开言路
    2021-02-05 09:22

    Can create the helper function :

    const isEmpty = inputObject => {
      return Object.keys(inputObject).length === 0;
    };
    

    Can use it like:

    let inputObject = {};
    console.log(isEmpty(inputObject))  // true.
    

    and

    inputObject = {name: "xyz"}; 
    console.log(isEmpty(inputObject)) // false
    

提交回复
热议问题