我有一个Ember计算属性,可以从ember模型中创建一个对象。
在其中一个对象属性中,我试图评估一个ID是否存在,以及在不使该属性为0而不是null的情况下。
计算属性如下:
SpecSortData: computed('customerfr', function() {
let newArray = [];
this.get('customerfr').forEach(function(x) {
function idCheck() {
if (x.data.speciality_id !== null) {
return x.data.specialty_id
} else {
return 0
}
};
let newData = {
specialtyID: x.data.specialty_id,
specialty: x.data.specialty_name,
customerID: x.data.customer_id,
tspecID: idCheck()
}
newArray.push(newData)
})
return newArray
}),
我无法正确评估idCheck()函数。 specialty_id字段是数字或null。如果它是一个数字,我希望tspecID是那个数字。在它为null的情况下,我希望tspecID为0。
上述函数当前将speciality_id编号添加到tspecID属性,但它不处理null情况,它们保持为null。如果我反转条件,我将所有tspecID属性设置为0。
我尝试了一些条件的变化,并总是最终得到:
我觉得我在评估或功能构建方面犯了一个基本错误,但我很难过。
答案 0 :(得分:4)
如果speciality_id
是假的,您可以使用logical OR ||
并取零。
BTW,speciality_id !== specialty_id
。
function idCheck() {
return x.data.speciality_id || 0;
}