Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript equivalent of $.isEmptyObject({}) for empty object

May I know what is the formal way of testing an Object (response from server), if it is an empty object?

Currently, my way of doing is utilising jQuery.

  this.http.post(url, data, {headers: headers}).then(
    result =>
     {
       if (!$.isEmptyObject(result)) {
        -- run code here --
       }else{
        -- log warning --
       }
     }

I dun think this is orthodox and would like to find a method that uses typescript only.

Thanks.

like image 795
Gavin Yap Avatar asked Aug 09 '16 17:08

Gavin Yap


1 Answers

Here's jquery's 2.1 implementation which should also work in TypeScript:

isEmptyObject: function( obj ) {
    var name;
    for ( name in obj ) {
        return false;
    }
    return true;
},

As you can see, it just tests to see if there are any enumerable properties. You could also do Object.keys(obj).length === 0.

like image 152
jfriend00 Avatar answered Sep 30 '22 00:09

jfriend00