admin管理员组文章数量:1399940
I'm using const in a function to define a few variables as follows
const printBlock.format({
name : this.matchedData.clientName,
date : this.matchedData.jobDate,
destination : this.matchedData.address,
apartment : this.matchedData.address2,
stateZip : this.matchedData.zipcode
})
From then, I'm printing out all of these things in order that they're declared. However, some of the data doesn't have an apartment number so it'll show up as:
John Doe
6/6/2018
135 Testdemo Avenue
null
NY 11111
Is it possible to use an if function within declaring the consts in order to make it so that:
if (this.matchedData.address2 == null){
//Do nothing
} else {
apartment : this.matchedData.address2,
}
I'm using const in a function to define a few variables as follows
const printBlock.format({
name : this.matchedData.clientName,
date : this.matchedData.jobDate,
destination : this.matchedData.address,
apartment : this.matchedData.address2,
stateZip : this.matchedData.zipcode
})
From then, I'm printing out all of these things in order that they're declared. However, some of the data doesn't have an apartment number so it'll show up as:
John Doe
6/6/2018
135 Testdemo Avenue
null
NY 11111
Is it possible to use an if function within declaring the consts in order to make it so that:
if (this.matchedData.address2 == null){
//Do nothing
} else {
apartment : this.matchedData.address2,
}
Share
Improve this question
edited Jun 6, 2018 at 20:51
Keyadun
asked Jun 6, 2018 at 19:45
KeyadunKeyadun
9151 gold badge10 silver badges15 bronze badges
1
-
5
const printBlock({…})
is a syntax error. What are you actually doing? – Bergi Commented Jun 6, 2018 at 19:50
4 Answers
Reset to default 3No, but you can use a ternary
var object = {
address: '1111',
apartment : this.matchedData.address2 ? "" : this.matchedData.address2
}
You could use Object.assign
and check with property and if not null
, take an object for assignment.
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 }
));
You can create your object first without the apartment entry and then add the apartment entry if it is not null...
const a = {
name : this.matchedData.clientName,
date : this.matchedData.jobDate,
destination : this.matchedData.address,
stateZip : this.matchedData.zipcode
};
if (this.matchedData.address2 !== null){
a.apartment : this.matchedData.address2;
}
const printBlock({...})
will throw an error because it isn't a valid way to initialize a constant. If printBlock
is a function, why not handle null values in the body of printBlock
?
function printBlock(obj) {
for (var prop in obj) {
if (obj[prop]) {
console.log(obj[prop]); // or do whatever you mean by 'print'
}
}
}
本文标签: if statementUsing ifelse to define const in JavascriptStack Overflow
版权声明:本文标题:if statement - Using ifelse to define const in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744212809a2595499.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论