And of course I want to do this code-wise. It's not that there isn't alternative to this problem I'm facing, just curious.
And of course I want to do this code-wise. It's not that there isn't alternative to this problem I'm facing, just curious.
There is has method in lodash library for this. It can even check for nested fields.
_.has(object, 'a');
_.has(object, 'a.b');
This question is quite old but it still can be useful to some people.
Now, there is an other way to do it which is recommended if you have non-hardcoded keys.
You have to go from this:
foo.hasOwnProperty("bar");
To this:
Object.prototype.hasOwnProperty.call(foo, "bar");
This update is particularly useful, especially if you use a linter like ESLint which has by default this rule in the "eslint:recommended" set of rules. This new way to do it is notably for security reasons. The whole explanation is available on this page.