0

I would like to know a quick and clean way to check if objects with just __ob__ or __proto__ properties are empty. Now I have to "guess" a value inside my object and null check that instead of the object itself. Example:

const myObject = objectFromDb.innerValue ? objectFromDb : mockObject;

I would love to write this instead:

const myObject = objectFromDb || mockObject;

but it will return objectFromDb even if the values have not yet been retrieved. I'm fine with using a ternary statement instead if the || one, but I'm not happy with having to null-check an inner object. I'm sure there has to be a better way of doing this.

Håkon
  • 9
  • 2
  • Is there a reason `objectFromDb` itself cannot be `null` until it's time to assign a real value? – Matt U May 25 '21 at 19:42
  • `__proto__` is not an own data property. It's a deprecated, inherited accessor. – Bergi May 25 '21 at 19:42
  • Try `Object.keys(objectFromDb).length > 0`? Not sure how Vue / your database treats `__ob__`. – Bergi May 25 '21 at 19:44
  • 1
    The question isn't specific enough what you mean. It looks like you describe empty reactive object, a screenshot from console could clarify this. Then the answer is to use Object.keys like suggested above, or Lodash isEmpty, or else. This is not specific to Vue. – Estus Flask May 25 '21 at 19:53
  • Thank you all for your comments! I guess my question could be a duplicate, but I think the fact that the object had `__ob__` and `__proto__` makes it a little different since I straight up ignored that question when I found it, thinking it was a different problem. Anyways @Bergi your solution worked :) – Håkon May 25 '21 at 20:19

1 Answers1

0

This worked:

Object.keys(objectFromDb).length > 0

(thanks to comment from Bergi)

Håkon
  • 9
  • 2