Search code examples
javascriptarraysobjectis-empty

javascript how to check if object which includes arrays is empty


I have an object, which itself includes arrays

how to check if such object is empty or not in javascript (or in Angular.js) ?

please see the image


Solution

  • You can create a function to see if the object has any enumerable properties:

    function isEmptyObject(obj) {
       if (!obj || typeof obj !== "object") {
          throw new Error("Must pass object");
       }
       return Object.keys(obj).length === 0;
    }
    

    Or, if you don't want the type checking:

    function isEmptyObject(obj) {
       return Object.keys(obj).length === 0;
    }