javascript - check if object is empty

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

    You were testing sellers which is not empty because it contains mergedSellerArray. You need to test sellers.mergedSellerArray

    let sellers = {
      "mergedSellerArray": {}
    };
    if (Object.keys(sellers.mergedSellerArray).length === 0 && sellers.mergedSellerArray.constructor === Object) {
      console.log("sellers is empty!");
    } else {
      console.log("sellers is not empty !");
    }

提交回复
热议问题