检查特定对象是否为空

如何检查对象是否为空?

例如:

private brand: Brand = new Brand();

我试过:

if (this.brand) {
console.log('is empty');
}

没用。

161466 次浏览

你可以这样使用 Object.keys:

class Brand { }
const brand = new Brand();


if (Object.keys(brand).length === 0) {
console.log("No properties")
}

如果要检查对象是否至少有一个 非空不明确属性:

  • 使用 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")
}

还可以使用 强大检查对象

if(_.isEmpty(this.brand)){
console.log("brand is empty")
}

使用 Object.keys(obj).length检查它是否为空。

产出 : 3

来源: < a href = “ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global _ Objects/Object/keys”rel = “ norefrer”> Object.keys()

Object.values(this.brand).some(b => b != null);

一个好的方法是有一个简短的功能,你可以在你的应用程序的任何地方使用:

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;
})();
};

如果你建立 ECMA 7 + 可以尝试 Object.entries(obj).length === 0 && obj.constructor === Object

小心 Object.keysArray.some解决方案,以防您的对象甚至没有初始化,值得 null

也要注意没有值得 undefined的钥匙。

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) === '{}'
Object.keys(myObject).length == 0

可以使用空属性创建 Map obj,size 可能不起作用。 对象可能不等于空或未定义

但是通过上面的代码,您可以查找一个对象是否真的是空的

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;
}

下面是两个最受欢迎的答案的比较,它们的含义略有不同:

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