JavaScript vérifie si l'objet est vide
function isObjectEmpty(obj) {
return Object.keys(obj).length === 0;
}
Grepper
function isObjectEmpty(obj) {
return 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;
// Today, end of 2021 near, I'd go for
const objIsEmpty = o => !Reflect.ownKeys( o ).length;
// Will catch also non-empties with Symbol keys, e.g.
const FOO = Symbol('foo');
const obj = { [FOO]: 'foo', };
objIsEmpty(obj); // false
objIsEmpty({}); // true