It depends on what you mean by "empty."
If you mean an object with no properties
If you mean you're getting:
[{}]
...then madox2's answer is a good way to check. Alternatively, you might have a function to do it that you can reuse:
function isEmptyObject(obj) {
for (const key in obj) {
if (Object.hasOwn(obj, key)) { // Remove this if you want to check for
// enumerable inherited properties
return false;
}
}
return true;
}
(Object.hasOwn was added in ES2022, but is easily polyfilled.)
If you mean [null] or similar
If you want to check specifically for undefined (note the === rather than ==) (but you won't get undefined from JSON):
if (myarray[0] === undefined)
Or specifically for null (note the === rather than ==):
if (myarray[0] === null)
Or for either of those (note the == rather than ===):
if (myarray[0] == null)
Or for any falsy value (0, "", NaN, null, undefined, or of course, false):
if (!myarray[0])
undefined, but JSON can't give you an array with anundefinedentry, as it doesn't have that concept.myarray[0]=='empty'tests whether the first array element is the string'empty'. That doesn't seem like a useful thing to try in the first place. Similarly,myarray.indexOf(0)tries to find the index of the value0. It seems you nee to ramp up on some JavaScript basics about values: eloquentjavascript.net/01_values.html, eloquentjavascript.net/04_data.html .