这是我的对象
{"seatName": seats[i].name,
"fare": seats[i].fare,
"passenger": [{
"name":"",
"gender": "",
// "email":this.props.authStore.email,
// "mobile": this.props.authStore.phone
}]
}在这里,我需要包括
"email":this.props.authStore.email,
"mobile": this.props.authStore.phone当(i === 0),而不是0时,对于其他,这些值不应包括在内!如何做到这一点?
我试过这样做:
(i === 0) ? "email": (i === 0) ? this.state.primaryUserEmail : "",但是它抛出了错误!
发布于 2020-10-15 01:02:25
尝试使用以下命令:
...(i === 0 && {email: this.props.authStore.email}),它将包括何时满足条件和何时不满足时删除!
发布于 2020-10-14 23:56:41
let data = {"seatName": "Joe",
"fare": 12,
"passenger": [{
"name":"",
"gender": "",
// "email":this.props.authStore.email,
// "mobile": this.props.authStore.phone
}]
}
/*
you can simply check if that passenger object has an email and mobile number and if they don't have you can then assign the number and email.
*/
if(!data.passenger[0]["email"]){
data.passenger[0]["email"] = "test@email.com";
}
if(!data.passenger[0]["mobile"]){
data.passenger[0]["mobile"] = 1234567890;
}
console.log(data)
/*
OUTPUT:
{
seatName: 'Joe',
fare: 12,
passenger: [ { name: '', gender: '', email: "test.email.com", mobile: 1234567890 } ]
}
*/https://stackoverflow.com/questions/64357005
复制相似问题