在我的calculatePercents()方法中,我應該得到0%而不是NULL%。
在我的console.log中,我得到的列印值是NULL
calculatePercents(/span>) {
this.v.global.total。 amount = this.v。 global.cash.amount this. v.global.sec.amount。
console.log("Cash => " JSON。 stringify(this.v。 global.total.amount))。)
console.log('------------------------'/span>)。
this.v.global.cash。 percent = (this.v.global.cash. amount /(this.v.global.cash. amount this.v。 global.sec.amount) * 100;
console.log(" Global cash " JSON. stringify(this.v。 global.cash.percent)。)
this.v.global.sec。 percent = (this.v.global.sec. amount /(this.v.global.cash. amount this.v。 global.sec.amount) * 100;
console.log(" NULL _00 ==>" JSON.stringify(this。 v.global.sec.percent)。)
var totPercent = (this.v.global.cash. percent this.v。 global.sec.percent)。)
this.v.global.total.percent = totPercent;
}
請問我怎樣才能得到一個0而不是NULL。
uj5u.com熱心網友回復:
用||,如果第一個值是假的(轉換為布林值),那么它將采取第二個,在這種情況下,它被用作默認值。
this.v。 global.total.percent= totPercent || 0;
uj5u.com熱心網友回復:
你可以使用||運算子來設定0
Eg:
this.v。 global.total.percent=(totPercent||0)。
對于更多的https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR
uj5u.com熱心網友回復:
你可以在你的方法中試試這個;
if(this.v。 global.cash.amount == null){
this.v.global。 cash.amount = 0。
}
if(this.v。 global.sec.amount == null){
this.v.global。 sec.amount = 0.
然后你可以這樣做;(你的方法)
calculatePercents(){
this.v.global.total. amount = this.v。 global.cash.amount this. v.global.sec.amount。
console.log("Cash => " JSON。 stringify(this.v。 global.total.amount))。)
console.log('------------------------'/span>)。
this.v.global.cash。 percent = (this.v.global.cash. amount /(this.v.global.cash. amount this.v。 global.sec.amount) * 100;
console.log(" Global cash " JSON. stringify(this.v。 global.cash.percent)。)
this.v.global.sec。 percent = (this.v.global.sec. amount /(this.v.global.cash. amount this.v。 global.sec.amount) * 100;
console.log(" NULL _00 ==>" JSON.stringify(this。 v.global.sec.percent)。)
var totPercent = (this.v.global.cash. percent this.v。 global.sec.percent)。)
this.v.global.total.percent= totPercent;
}
而你的可變數型別可以是 "let"。 簡單的代碼:
let a = null。
if(a==null){
a=0
}
console.log(a)。
//輸出。0
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/323559.html
標籤:


