Given that this.state.errors is an object you can do this,
//when this.state.errors object is empty
if (Object.keys(this.state.errors).length === 0) {
this.props.updateUser(user);
this.props.navigation.goBack();
}
Object.keys will return an array or all the keys from the object this.state.errors. Then you can check the length of that array to determine if it is an empty object or not.
Given that this.state.errors is an object you can do this,
//when this.state.errors object is empty
if (Object.keys(this.state.errors).length === 0) {
this.props.updateUser(user);
this.props.navigation.goBack();
}
Object.keys will return an array or all the keys from the object this.state.errors. Then you can check the length of that array to determine if it is an empty object or not.
Actually, You need to first check this.state.errors is exist or not and then for object is null or not
if (this.state.errors && !Object.keys(this.state.errors)) {
this.props.updateUser(user);
this.props.navigation.goBack();
}
Videos
What is the fastest way to check if an object is empty in React?
Why should I use Lodash's isEmpty method in my React project?
Are there any performance considerations when using JSON.stringify() to check if an object is empty?
For ECMAScript5 (not supported in all browsers yet though), you can use:
Object.keys(obj).length === 0
I'm assuming that by empty you mean "has no properties of its own".
// Speed up calls to hasOwnProperty
var hasOwnProperty = Object.prototype.hasOwnProperty;
function isEmpty(obj) {
// null and undefined are "empty"
if (obj == null) return true;
// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length > 0) return false;
if (obj.length === 0) return true;
// If it isn't an object at this point
// it is empty, but it can't be anything *but* empty
// Is it empty? Depends on your application.
if (typeof obj !== "object") return true;
// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and valueOf enumeration bugs in IE < 9
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) return false;
}
return true;
}
Examples:
isEmpty(""), // true
isEmpty(33), // true (arguably could be a TypeError)
isEmpty([]), // true
isEmpty({}), // true
isEmpty({length: 0, custom_property: []}), // true
isEmpty("Hello"), // false
isEmpty([1,2,3]), // false
isEmpty({test: 1}), // false
isEmpty({length: 3, custom_property: [1,2,3]}) // false
If you only need to handle ECMAScript5 browsers, you can use Object.getOwnPropertyNames instead of the hasOwnProperty loop:
if (Object.getOwnPropertyNames(obj).length > 0) return false;
This will ensure that even if the object only has non-enumerable properties isEmpty will still give you the correct results.
You can use a for…in loop with an Object.hasOwn (ECMA 2022+) test to check whether an object has any own properties:
Copyfunction isEmpty(obj) {
for (const prop in obj) {
if (Object.hasOwn(obj, prop)) {
return false;
}
}
return true;
}
If you also need to distinguish {}-like empty objects from other objects with no own properties (e.g. Dates), you can do various (and unfortunately need-specific) type checks:
Copyfunction isEmptyObject(value) {
if (value == null) {
// null or undefined
return false;
}
if (typeof value !== 'object') {
// boolean, number, string, function, etc.
return false;
}
const proto = Object.getPrototypeOf(value);
// consider `Object.create(null)`, commonly used as a safe map
// before `Map` support, an empty object as well as `{}`
if (proto !== null && proto !== Object.prototype) {
return false;
}
return isEmpty(value);
}
Note that comparing against Object.prototype like in this example will fail to recognize cross-realm objects.
Do not use Object.keys(obj).length. It is O(N) complexity because it creates an array containing all the property names only to get the length of that array. Iterating over the object accomplishes the same goal but is O(1).
For compatibility with JavaScript engines that don’t support ES 2022+, const can be replaced with var and Object.hasOwn with Object.prototype.hasOwnProperty.call:
Copyfunction isEmpty(obj) {
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true
}
Many popular libraries also provide functions to check for empty objects:
jQuery:
CopyjQuery.isEmptyObject({}); // true
lodash:
Copy_.isEmpty({}); // true
Underscore:
Copy_.isEmpty({}); // true
Hoek:
CopyHoek.deepEqual({}, {}); // true
ExtJS:
CopyExt.Object.isEmpty({}); // true
AngularJS (version 1):
Copyangular.equals({}, {}); // true
Ramda:
CopyR.isEmpty({}); // true
If ECMAScript 5 support is available, you can use Object.keys():
Copyfunction isEmpty(obj) {
return Object.keys(obj).length === 0;
}
For ES3 and older, there's no easy way to do this. You'll have to loop over the properties explicitly:
Copyfunction isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
render() {
return (
<div>
<div>{!!(this.myvar)?this.myvar:"whatever you want"}</div>//
</div>
);
}
!!: check for undefined, null, and empty value
The provided solution runs afoul of eslint's no-extra-boolean-casts rule.
An alternate method that makes eslint happy would look like this:
render() {
return (
<div>
<div>{!this.myvar ? "whatever you want" : this.myvar}</div>//
</div>
);
}