js 判断对象是否为空

208 阅读1分钟
  • 1、JSON.stringify()
const obj = {}
const bool = JSON.stringify(obj) === "{}"
console.log(bool)
  • 2、for in遍历
const obj = {}
let bool
for (var k in obj) {
	bool = k ? false : true
}
  • 3、Object.keys()
const obj = {}
let bool
bool = Object.keys(obj).length ? false : true

4、Object.getOwnPropertyNames()

const obj = {}
let bool
bool = Object.getOwnPropertyNames(obj).length ? false : true