我有一位价值不同的客户:
- 如果客户 = 是,则分数 = 10
- 如果客户 = 否,则得分 = 5
- 如果客户为空,则分数 = 0
对于第一个和第二个条件,我的 json 结构如下:
json:
fields:
customer [1]:
0 {3}:
self: aaa
value: yes
id: 111
对于最后一个条件,我的 json 结构如下:
json:
fields:
customer:null
我正在尝试做这样的事情:
var customer = json.fields.customer[0].value ;
var score3 = 0;
if(typeof customer == 'string'){
if(customer === "Yes"){
score3 = +10;
}
else if(customer === "No"){
score3 = +5;
}
}
else{
score3 = 0;
}
但我有一个问题,他说:“无法读取属性‘0’”
谢谢你的帮助
您没有为客户检查 null
var customer =
json.fields.customer != null && json.fields.customer.length > 0 ? json.fields.customer[0].value : null;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)