有一个条件,但是很麻烦,怎么减少呢?或者编写一个函数,将有一个上诉。
if (obj.Language.code == "ru") {
if (obj.Tariff.typeCalc != 1) {
if (obj.Price.Discount) {
t = t + getText(obj.Language.code, 'PriceWithDiscount', [Round(obj.Price.Itog), final_currency]);
}
else {
t = t + getText(obj.Language.code, 'PriceNoDiscount', [Round(obj.Price.Itog), final_currency]);
}
}
else {
if (obj.Price.Discount) {
t = t + getText(obj.Language.code, 'PriceWithDiscount', [Round(obj.Price.Itog), obj.Currency.symbol]);
}
else {
t = t + getText(obj.Language.code, 'PriceNoDiscount', [Round(obj.Price.Itog), obj.Currency.symbol]);
}
}
设计
if (obj.Language.code == "ru") {
}
else {
}
会经常见面,不想堆积
if (obj.Language.code == "ru") {
if (obj.Price.Discount) {
t = t + getText(obj.Language.code, 'PriceWithDicountOutCity', [obj.Price.Itog, final_currency, Round(obj.Len.value, final_currency), nextkmprice]);
}
else {
t = t + getText(obj.Language.code, 'PriceOutCity', [obj.Price.Itog, final_currency, Round(obj.Len.value, final_currency), nextkmprice]);
}
}
else {
if (obj.Price.Discount) {
t = t + getText(obj.Language.code, 'PriceWithDicountOutCity', [obj.Price.Itog, obj.Currency.symbol, Round(obj.Len.value, obj.Currency.symbol), nextkmprice]);
}
else {
t = t + getText(obj.Language.code, 'PriceOutCity', [obj.Price.Itog, obj.Currency.symbol, Round(obj.Len.value, obj.Currency.symbol), nextkmprice]);
}
}
最佳答案
您可以采用单个外部检查和两个条件。
if (obj.Tariff.typeCalc != 1) {
t += getText(
obj.Language.code,
obj.Price.Discount ? 'PriceWithDiscount' : 'PriceNoDiscount',
[
Round(obj.Price.Itog),
obj.Language.code == "ru" ? final_currency : obj.Currency.symbol
]
);
}
对于最后一个问题,您可以预先存储currency
并使用变量而不是多个条件语句。
var currency = obj.Language.code == "ru" ? final_currency : obj.Currency.symbol;
t += getText(
obj.Language.code,
obj.Price.Discount ? 'PriceWithDicountOutCity' : 'PriceOutCity',
[
obj.Price.Itog,
currency,
Round(obj.Len.value, currency),
nextkmprice
]
);
关于javascript - 将其放入函数JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52643927/