如果 x = null,则将对象 属性 分配给值
Assign object property to value if x = null
我有一个 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 字段是数字或空值。如果它是一个数字,我希望 tspecID 是那个数字。在它为 null 的情况下,我希望 tspecID 为 0。
上述函数当前将 speciality_id 编号添加到 tspecID 属性,但它不处理 null 情况,它们仍然为 null。如果我反转条件,我会将所有 tspecID 属性设置为 0。
我尝试了几种不同的条件,但总是以以下任一方式结束:
- specID = specialtyID with null cases remaining null
- tspecID = 始终为 0
我觉得我在评估或函数构造中犯了一个基本错误,但我很困惑。
您可以使用 logical OR ||
并在 speciality_id
为假时取零。
顺便说一句,speciality_id !== specialty_id
。
function idCheck() {
return x.data.speciality_id || 0;
}
我有一个 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 字段是数字或空值。如果它是一个数字,我希望 tspecID 是那个数字。在它为 null 的情况下,我希望 tspecID 为 0。
上述函数当前将 speciality_id 编号添加到 tspecID 属性,但它不处理 null 情况,它们仍然为 null。如果我反转条件,我会将所有 tspecID 属性设置为 0。
我尝试了几种不同的条件,但总是以以下任一方式结束:
- specID = specialtyID with null cases remaining null
- tspecID = 始终为 0
我觉得我在评估或函数构造中犯了一个基本错误,但我很困惑。
您可以使用 logical OR ||
并在 speciality_id
为假时取零。
顺便说一句,speciality_id !== specialty_id
。
function idCheck() {
return x.data.speciality_id || 0;
}