JS é objeto vazio
function isObjectEmpty(obj) {
return Object.keys(obj).length === 0;
}
Grepper
function isObjectEmpty(obj) {
return Object.keys(obj).length === 0;
}
if(Object.keys(obj).length === 0) {
}
function isEmptyObject(obj) {
return !Object.keys(obj).length;
}
const empty = {};
/* -------------------------
Plain JS for Newer Browser
----------------------------*/
Object.keys(empty).length === 0 && empty.constructor === Object
// true
/* -------------------------
Lodash for Older Browser
----------------------------*/
_.isEmpty(empty)
// true
const emptyObject = {
}
// Using keys method of Object class
let isObjectEmpty = (object) => {
return Object.keys(object).length === 0;
}
console.log(isObjectEmpty(emptyObject)); // true
// Using stringify metod of JSON class
isObjectEmpty = (object) => {
return JSON.stringify(object) === "{}";
}
console.log(isObjectEmpty(emptyObject)); // true
> !!Object.keys(obj).length;