检查变量/键是否存在 [英] Check if the variable/key exist
本文介绍了检查变量/键是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个条件不同的有价值客户:
I have a value customer with different condition :
- 如果客户=是,则得分= 10
- 如果客户=否,则得分= 5
- 如果客户为空,则得分= 0
对于第一个和第二个条件,我的json结构是这样的:
My json structure is like this for the first and second condition :
json:
fields:
customer [1]:
0 {3}:
self: aaa
value: yes
id: 111
最后一个条件是我的json结构:
And my json structure is like this for the last condition :
json:
fields:
customer:null
我正在尝试做这样的事情:
I'm trying to do something like this :
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'"的问题
But I have a problem who says: "Cannot read property '0'"
感谢您的帮助
推荐答案
您没有为客户检查null
You did not check null for customer
var customer =
json.fields.customer != null && json.fields.customer.length > 0 ? json.fields.customer[0].value : null;
这篇关于检查变量/键是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文