0

Now I am using this code the check if result is null then return:

let data = this.props.channel.channel;
        if(!data || data == {} || data == undefined){
            return (<div></div>);
        }

but from my debug output seems it did not work, what should I do to fix it?

enter image description here

Dolphin
  • 7,028
  • 18
  • 58
  • 139
  • 1
    `data == {}` doesn't make any sense. It will be `false` no matter what because objects are compared by reference. – connexo Apr 11 '21 at 07:59

2 Answers2

1

data == {} doesn't make any sense. It will be false no matter what because objects are compared by reference.

If you need to check for an empty object, you have several methods at your disposal:

JSON.stringify(data) === '{}'

or

Object.getOwnPropertyNames(data).length === 0 // will also count non-enumerable properties!

or

Object.keys(data).length === 0
connexo
  • 41,035
  • 12
  • 60
  • 87
1

If you're aiming to check for an empty object, you can use this:

Object.keys(data).length // outputs 0 if object is empty

This converts the object keys to an array, which you can then use to check how many items the object had.

More info on Object.keys() here.

Californium
  • 340
  • 2
  • 10