Apna Garage
PHP, WordPress Codes
Home
WordPress
Friday, 29 June 2018
isEmpty in JavaScript
The best way to check if an object is empty is by using a utility function like the one below.
function isEmpty(obj) { for(var key in obj) { if(obj.hasOwnProperty(key)) return false; } return true; }
So if you have an empty object, you can check whether it is empty by using the above function.
var myObj = {}; // Empty Object if(isEmpty(myObj)) { // Object is empty (Would return true in this example) } else { // Object is NOT empty }
No comments:
Post a Comment
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment