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
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;
}
Below code(jQuery.isEmptyObject(anyObject) function is already provided) works perfectly fine, no need to write one of your own.
// works for any Object Including JSON(key value pair) or Array.
// var arr = [];
// var jsonObj = {};
if (jQuery.isEmptyObject(anyObjectIncludingJSON))
{
console.log("Empty Object");
}
Just test if the array is empty.
$.getJSON(url,function(json){
if ( json.length == 0 ) {
console.log("NO DATA!")
}
});
Videos
Try below code snippet:-
if(!Object.keys(response.data).length){
console.log("no data found");
}
If you are getting data as empty object like data: {}, then you should check if there is any key inside the object or not.
You can check the number of keys/properties with Object.keys(response.data).length === 0 or less efficiently with JSON.stringify({}) == JSON.stringify(response.data)
You were testing sellers which is not empty because it contains mergedSellerArray. You need to test sellers.mergedSellerArray
let sellers = {
"mergedSellerArray": {}
};
if (Object.keys(sellers.mergedSellerArray).length === 0 && sellers.mergedSellerArray.constructor === Object) {
console.log("sellers is empty!");
} else {
console.log("sellers is not empty !");
}
If you are using lodash library, you have an elegant way to check an empty object, array, map or a set. I presume you are aware of ES6 Import statement.
import {isEmpty} from "lodash"
let obj = {};
console.log(isEmpty(obj)); //Outputs true.
let arr = [];
console.log(isEmpty(arr)); //Outputs true.
obj.name="javascript";
console.log(isEmpty(obj)); //Outputs false.
So, for your code,
isEmpty(mergedSellerArray); //will return true if object is not empty.
Hope this answer helped.
you can check if it has any keys:
Object.keys(dataJson).length
To check if the responseText is an empty string:
dataJson === '' // true if no JSON data (it's an empty string)
To check if there is it is an empty object:
dataJson === '{}' // true if empty
You may also want to check if it is an empty array, depending on your use case:
dataJson === '[]' // true if empty
To check if the parsed object is empty:
const data = JSON.parse(dataJson);
!!Object.keys(data).length // false if empty
Use $.isArray() to check whether an object is an array. Then you can check the truthness of the length property to see whether it is empty.
if( !$.isArray(siteArray) || !siteArray.length ) {
//handler either not an array or empty array
}
Two empty arrays are not the same as one another, for they are not the same object.
var a = [];
if (a === []){
// This will never execute
}
Use if (siteArray.length==0) to see if an array is empty, or more simply if (!siteArray.length)
function isArrayEmpty(array) {
return array.filter(function(el) {
return !jQuery.isEmptyObject(el);
}).length === 0;
}
jsFiddle Demo
Passes all of your tests.
A pure JavaScript solution would be to replace !jQuery.isEmptyObject(el) with Object.keys(el).length !== 0
Edit: Using Array.prototype.every
function isArrayEmpty(array) {
return array.every(function(el) {
return jQuery.isEmptyObject(el);
});
}
For those playing at home, a non jQuery solution:
var test2 = [{a: 1},{},{}] ; //This is empty
function isEmpty(val) {
var len = val.length,
i;
if (len > 0) {
for (i = 0; i < len; ++i) {
if (!emptyObject(val[i])) {
return false;
}
}
}
return true;
}
function emptyObject(o) {
for (var key in o) {
if (o.hasOwnProperty(key)) {
return false;
}
}
return true;
}
console.log(isEmpty(test2));
i don't know what is you meaning about empty object, but if you consider
{}
as a empty object, i suppose you use the code below
var obj = {};
if (Object.keys(obj).length === 0) {
alert('empty obj')
}
Use Array's length property:
// note: you don't even need '== 0'
if (data.objects.length == 0) {
alert("Empty");
}
else {
alert("Not empty");
}