JavaScript Periksa apakah objek kosong
function isObjectEmpty(obj) {
return Object.keys(obj).length === 0;
}
Grepper
function isObjectEmpty(obj) {
return Object.keys(obj).length === 0;
}
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
// because Object.entries(new Date()).length === 0;
// we have to do some additional check
Object.entries(obj).length === 0 && obj.constructor === Object
function isEmpty(obj) { return Object.keys(obj).length === 0;}
if (typeof value !== 'undefined' && value) {
//deal with value'
};