The observable array or object (obtained via observe()
) to check.
true
if the array has length 0 or the object has no own enumerable properties, false
otherwise.
const items = proxy([]);
// Reactively display a message if the items array is empty
$('div', () => {
if (isEmpty(items)) {
$('p', 'i:No items yet!');
} else {
onEach(items, item=>$('p:'+item));
}
});
// Adding an item will automatically remove the "No items yet!" message
setInterval(() => {
if (!items.length || Math.random()>0.5) items.push('Item');
else items.length = 0;
}, 1000)
Reactively checks if an observable array or object is empty.
This function not only returns the current emptiness state but also establishes a reactive dependency. If the emptiness state of the
proxied
object or array changes later (e.g., an item is added to an empty array, or the last property is deleted from an object), the scope that calledisEmpty
will be automatically scheduled for re-evaluation.