1
如何檢查對象是否爲空?檢查打字稿中的特定對象是否爲空
例如:
private brand:Brand = new Brand();
我想:
if(this.brand)
{
console.log('is empty');
}
不工作。
如何檢查對象是否爲空?檢查打字稿中的特定對象是否爲空
例如:
private brand:Brand = new Brand();
我想:
if(this.brand)
{
console.log('is empty');
}
不工作。
如果你想檢查一個對象是否爲空,你可以創建一個像這樣的函數。
isEmptyObject(obj) {
for(var prop in obj) {
if (obj.hasOwnProperty(prop)) {
return false;
}
}
return true;
}
,並通過this.brand
作爲參數傳遞給這個函數:
if(this.isEmptyObject(this.brand))
{
console.log("brand is empty")
}
您還可以使用lodash檢查
if(_.isEmpty(this.brand)){
console.log("brand is empty")
}
要檢查,如果品牌是一個空的對象,而對象屬性還是未定義? – adiga
也許它不工作,因爲你的條件測試是否this.brand不是空的:)要麼嘗試如果(!this.brand),要麼做console.log('不是空的') – JustAndrei
@adiga我想檢查屬性這個對象是空的。 – Unfra