如何检查一个对象是否为空?
例:
private brand: Brand = new Brand();
我试过:
if (this.brand) {
console.log('is empty');
}
不起作用。
如何检查一个对象是否为空?
例:
private brand: Brand = new Brand();
我试过:
if (this.brand) {
console.log('is empty');
}
不起作用。
Object.keys
:
class Brand { }
const brand = new Brand();
if (Object.keys(brand).length === 0) {
console.log("No properties")
}
如果您想检查对象是否至少有一个非null、非undefined属性:
Object.values()
将对象的所有值存储在数组中some
检查其中至少一个值是否存在const hasValues =
(obj) => Object.values(obj).some(v => v !== null && typeof v !== "undefined")
class Brand { }
const brand = new Brand();
if (hasValues(brand)) {
console.log("This won't be logged")
}
brand.name = null;
if (hasValues(brand)) {
console.log("Still no")
}
brand.name = "Nike";
if (hasValues(brand)) {
console.log("This object has some non-null, non-undefined properties")
}
以下是两个最流行答案的比较,它们确实有略微不同的含义:
let o1 = {}
console.log(JSON.stringify(o1) === '{}')
console.log(Object.keys(o1).length === 0)
// true
// true
let o2 = { p: undefined }
console.log(JSON.stringify(o2) === '{}')
console.log(Object.keys(o2).length === 0)
// true
// false
let o3 = { l: null }
console.log(JSON.stringify(o3) === '{}')
console.log(Object.keys(o3).length === 0)
// false
// false
if(_.isEmpty(this.brand)){
console.log("brand is empty")
}
Object.keys(myObject).length == 0
一个 Map 对象可以使用空属性创建,并且大小可能不起作用。对象可能不等于空或未定义。let contacts = {};
if(Object.keys(contacts).length==0){
console.log("contacts is an Empty Object");
}else{
console.log("contacts is Not an Empty Object");
}
这是我知道的最快的结构,尽管它使用了有点令人困惑的for...in
循环,但它并不真正执行循环(根据我的测试,它比Object.keys
快约2倍)
export function isObjectEmpty(object: Record<string, unknown>): boolean {
for (const property in object) {
// if any enumerable property is found object is not empty
return false;
}
return true;
}
null
,则需要小心使用Object.keys
和Array.some
解决方案。undefined
。"Original Answer"翻译成"最初的回答"。
const objNotInitialized = null;
console.log(Object.keys(objNotInitialized));
你可以在这种情况下添加额外的检查,从而得到最终解决方案:
function isEmpty(obj) {
return !obj || !Object.keys(obj).some(x => obj[x] !== void 0);
}
console.log(isEmpty({
x: void 0,
}));
console.log(isEmpty(null));
console.log(isEmpty({
key: 'value',
}));
Object.values
:
function isEmpty(obj) {
return !obj || !Object.values(obj).some(x => x !== void 0);
}
console.log(isEmpty({
x: void 0,
}));
console.log(isEmpty(null));
console.log(isEmpty({
key: 'value',
}));
const obj = {};
// Using Object.keys to loop on the object keys and count them up
if (!Object.keys(obj).length) {
console.log('#1 obj is empty');
}
// What if a key worth undefined ?
const objWithUndefinedKey = {
x: void 0,
};
// Using Object.keys is not enough, we have to check the value behind to remove
// undefined values
if (!Object.keys(objWithUndefinedKey).some(x => objWithUndefinedKey[x] !== void 0)) {
console.log('#2 obj is empty');
}
// Or more elegant using Object.values
if (!Object.values(objWithUndefinedKey).some(x => x !== void 0)) {
console.log('#3 obj is empty');
}
// Alternative is to use for ... in
let empty = true;
for (key in objWithUndefinedKey) {
if (objWithUndefinedKey[key] !== void 0) {
empty = false;
}
}
if (empty) {
console.log('#4 obj is empty');
}
JSON.stringify(this.brand) === '{}'
export const isEmpty = (obj) => {
return obj === null || undefined
? true
: (() => {
for (const prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true;
})();
};