我有一个条件不同的有价值的客户:
如果客户=是,则得分= 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;