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
把JS对象转换为JSON字符串再比较
let a = {name: 'lee'}; if(JSON.stringify(a) === '{}'){ console.log('这是空对象'); }else { console.log('这不是是空对象'); }
利用for in的特性,如果不是空对象,那一定会进循环部分里面
let a = {}; let isEmptyObj = true; for(let key in a) { isEmptyObj = false; } if(isEmptyObj === true) { console.log('这是空对象'); }else { console.log('这不是空对象'); }
Object.keys() 方法会返回一个由给定对象的自身可枚举属性组成的数组。 如果我们的对象为空,他会返回一个空数组
let a = {name: 'lee'}; if(Object.keys(a).length === 0) { console.log('这是一个空对象'); }else { console.log('这不是一个空对象'); }
我以arguments对象为例
Array.prototype.slice.apply(arguments);
Array.from(arguments);
...
let newArr = [...arguments];
The text was updated successfully, but these errors were encountered:
No branches or pull requests
1、如何判断一个对象为空
1.1 使用JSON.stringify()
把JS对象转换为JSON字符串再比较
1.2 使用for in
利用for in的特性,如果不是空对象,那一定会进循环部分里面
1.3 使用ES6 Object.keys
Object.keys() 方法会返回一个由给定对象的自身可枚举属性组成的数组。 如果我们的对象为空,他会返回一个空数组
2、如何将类数组对象转换为数组
我以arguments对象为例
2.1、使用slice与apply
2.2、使用Array.from()
2.3、使用展开运算符
...
The text was updated successfully, but these errors were encountered: