javascript - check if object is empty

前端 未结 5 1024
感情败类
感情败类 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:15

    If you are using lodash library, you have an elegant way to check an empty object, array, map or a set. I presume you are aware of ES6 Import statement.

    import {isEmpty} from "lodash"
    
    let obj = {};
    console.log(isEmpty(obj)); //Outputs true.
    
    let arr = [];
    console.log(isEmpty(arr)); //Outputs true.
    
    obj.name="javascript";
    console.log(isEmpty(obj)); //Outputs false.
    

    So, for your code,

    isEmpty(mergedSellerArray); //will return true if object is not empty.
    

    Hope this answer helped.

提交回复
热议问题