我需要在service_ids中将if条件设置为空或null。 如果为空或为null,则不应传递service_ids变量。我该怎么办?
const formData = {
car: form.value.car,
remarks: form.value.remarks,
if(form.value.service_ids !== null || form.value.service_ids !== []) {
service_ids: form.value.service_ids.map((a: Service) => a.service_id ? a.service_id : []),
}
}
答案 0 :(得分:0)
怎么样
const formData = {
car: form.value.car,
remarks: form.value.remarks
}
if(form.value.service_ids !== null || form.value.service_ids !== []) {
formData.service_ids = form.value.service_ids.map(
(a: Service) => a.service_id ? a.service_id : []
);
}
答案 1 :(得分:0)
var {car,remarks,service_ids} = form.value;
service_ids = service_ids && service_ids.length > 0
? service_ids.map((a:Service) => a.service_id ? a.service_id : undefined).filter(val=>!(!val || val === ""))
: []
var formData = {
car,
remarks,
service_ids
}