We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
如何判断一个对象是否为空对象?即是否是 {} 这样的对象。
{}
Object.keys
const isObject = obj => Object.prototype.toString.call(obj) === '[object Object]'; const isEmptyObject = obj => isObject(obj) ? Object.keys(obj).length === 0 : false; isEmptyObject({}); // true isEmptyObject({a: 1}); // false
const isEmptyObject = obj => { for (let key in obj) { if (obj.hasOwnProperty(key)) { return false } } return true; }; isEmptyObject({}); // true isEmptyObject({a: 1}); // false
Object.defineProperty(Object.prototype, 'isEmptyObject', { writable: false, enumerable: false, configurable: false, value: function() { for (let key in this) { if (this.hasOwnProperty(key)) { return false } } return true; } }); // 注意:字面量对象调用时要将表达式用小括号包起来 ({}.isEmptyObject()); // true ({a: 1}.isEmptyObject()); // false let obj = {}; obj.isEmptyObject(); // true
JSON.stringify
const isEmptyObject = obj => JSON.stringify(obj) === '{}'; isEmptyObject({}); // true isEmptyObject({a: 1}); // false
The text was updated successfully, but these errors were encountered:
No branches or pull requests
如何判断一个对象是否为空对象?即是否是
{}
这样的对象。方法 1:ES6
Object.keys
判断长度方法 2:for in 循环判断
方法 3:方法 2 的变种
方法 4:
JSON.stringify
转换判断The text was updated successfully, but these errors were encountered: