EN
JavaScript - how to check if object is empty?
7 points
Using JavaScript it is possible to check if object is empty in following way.
xxxxxxxxxx
1
function isEmpty(object) {
2
for (var name in object) {
3
if (object.hasOwnProperty(name)) {
4
return false;
5
}
6
}
7
8
return true;
9
}
10
11
// Usage example:
12
13
var object1 = { };
14
15
var object2 = {
16
items: [ ]
17
};
18
19
var object3 = {
20
result: false
21
};
22
23
var object4 = {
24
ptint: function() { /* nothing here... */ }
25
};
26
27
console.log(isEmpty(object1)); // true
28
console.log(isEmpty(object2)); // false
29
console.log(isEmpty(object3)); // false
30
console.log(isEmpty(object4)); // false