我在函数中使用 const 来定义一些变量,如下所示
const printBlock.format({
name : this.matchedData.clientName,
date : this.matchedData.jobDate,
destination : this.matchedData.address,
apartment : this.matchedData.address2,
stateZip : this.matchedData.zipcode
})
从那时起,我打印出所有这些东西,以便他们宣布。但是,有些数据没有公寓号,所以它会显示为:
John Doe
2018年6月6日
135 Testdemo Avenue
空
NY 11111
是否可以在声明争用中使用if函数以使其成为:
if (this.matchedData.address2 == null){
//Do nothing
} else {
apartment : this.matchedData.address2,
}
答案 0 :(得分:1)
不,但您可以使用ternary
var object = {
address: '1111',
apartment : this.matchedData.address2 ? "" : this.matchedData.address2
}
答案 1 :(得分:1)
您可以使用Object.assign
并检查属性,如果不是null
,请使用对象进行分配。
printBlock(Object.assign(
{
name: this.matchedData.clientName,
date: this.matchedData.jobDate,
destination: this.matchedData.address,
apartment: this.matchedData.address2,
stateZip: this.matchedData.zipcode
},
this.matchedData.address2 === null || { apartment: this.matchedData.address2 }
));
答案 2 :(得分:1)
您可以在没有公寓入口的情况下首先创建对象,然后添加公寓条目(如果它不为空...)
<ngb-accordion class="accordion accordion-alternate" (panelChange)="onPanelChange($event, source)" [activeIds]="source.relevantTopCount > 0 ? source.id : ''">
<ngb-panel class="source-accordion" [disabled]="source.docCount === 0" [id]="source.id">
</ngb-panel>
</ngb-accordion>
答案 3 :(得分:0)
"use strict";
let myArray = [1, 2, 3, 4, 5];
function notSplice(array, start, end, ...items) {
array.splice.apply(array, [start, end, ...items]);
return array;
}
myArray = notSplice([...myArray, 10], 2, 0, 'hello world');
console.log(myArray);
会抛出错误,因为它不是初始化常量的有效方法。如果const printBlock({...})
是一个函数,为什么不在printBlock
的主体中处理空值?
printBlock