因此,我尝试检查某些JSON数据是否包含某个键,如果包含,则显示该键值,但我一直收到错误"TypeError: Cannot read property 'CurrentTextValue' of undefined
如何检查某个键是否存在,以及它是否显示其值。
var carj = '[{ "Description": "SAAB 900 S CONVERTIBLE", "RegistrationYear": "1993", "CarMake": { "CurrentTextValue": "SAAB" }, "CarModel": { "CurrentTextValue": "900 S CONVERTIBLE" }, "MakeDescription": { "CurrentTextValue": "SAAB" }, "ModelDescription": { "CurrentTextValue": "900 S CONVERTIBLE" }, "EngineSize": { "CurrentTextValue": "1985" }, "BodyStyle": { "CurrentTextValue": "Motorbike" }, "FuelType": { "CurrentTextValue": "PETROL" }, "Variant": "", "Colour": "BLACK", "VehicleIdentificationNumber": "YS3AD75S1P7004905", "KType": "", "EngineNumber": "B202S3M03P032338" }]';
carj2 = $.parseJSON(carj);
if(carj2[0].NumberOfDoors.CurrentTextValue != '') {
console.log(carj2[0].NumberOfDoors.CurrentTextValue);
} else {
console.log('i dont have doors');
}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
发布于 2020-06-26 21:14:32
您需要首先找到NumberOfDoors,然后再查找值
在这里,我使用一个三元运算符作为输出
const carj = '[{ "Description": "SAAB 900 S CONVERTIBLE", "RegistrationYear": "1993", "CarMake": { "CurrentTextValue": "SAAB" }, "CarModel": { "CurrentTextValue": "900 S CONVERTIBLE" }, "MakeDescription": { "CurrentTextValue": "SAAB" }, "ModelDescription": { "CurrentTextValue": "900 S CONVERTIBLE" }, "EngineSize": { "CurrentTextValue": "1985" }, "BodyStyle": { "CurrentTextValue": "Motorbike" }, "FuelType": { "CurrentTextValue": "PETROL" }, "Variant": "", "Colour": "BLACK", "VehicleIdentificationNumber": "YS3AD75S1P7004905", "KType": "", "EngineNumber": "B202S3M03P032338" }]';
const carj2 = $.parseJSON(carj);
const NumberOfDoors = carj2[0].NumberOfDoors;
console.log( NumberOfDoors ? NumberOfDoors.CurrentTextValue : 'I don\'t have doors');<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2020年最新的JS可以做Optional Chaining
注意["NumberOfDoors"]?中的?
const carj = '[{ "Description": "SAAB 900 S CONVERTIBLE", "RegistrationYear": "1993", "CarMake": { "CurrentTextValue": "SAAB" }, "CarModel": { "CurrentTextValue": "900 S CONVERTIBLE" }, "MakeDescription": { "CurrentTextValue": "SAAB" }, "ModelDescription": { "CurrentTextValue": "900 S CONVERTIBLE" }, "EngineSize": { "CurrentTextValue": "1985" }, "BodyStyle": { "CurrentTextValue": "Motorbike" }, "FuelType": { "CurrentTextValue": "PETROL" }, "Variant": "", "Colour": "BLACK", "VehicleIdentificationNumber": "YS3AD75S1P7004905", "KType": "", "EngineNumber": "B202S3M03P032338" }]';
const carj2 = $.parseJSON(carj);
const NumberOfDoors = carj2[0].NumberOfDoors?.CurrentTextValue || 0
console.log(NumberOfDoors || 'I don\'t have doors');<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
发布于 2020-06-26 21:23:19
var carj = '[{ "Description": "SAAB 900 S CONVERTIBLE", "RegistrationYear": "1993", "CarMake": { "CurrentTextValue": "SAAB" }, "CarModel": { "CurrentTextValue": "900 S CONVERTIBLE" }, "MakeDescription": { "CurrentTextValue": "SAAB" }, "ModelDescription": { "CurrentTextValue": "900 S CONVERTIBLE" }, "EngineSize": { "CurrentTextValue": "1985" }, "BodyStyle": { "CurrentTextValue": "Motorbike" }, "FuelType": { "CurrentTextValue": "PETROL" }, "Variant": "", "Colour": "BLACK", "VehicleIdentificationNumber": "YS3AD75S1P7004905", "KType": "", "EngineNumber": "B202S3M03P032338" }]';
carj2 = $.parseJSON(carj);
// Method 1
if ("numberOfDoors" in carj2[0]) {
console.log(carj2[0].NumberOfDoors.CurrentTextValue);
} else {
console.log('i dont have doors');
}
// Method 2
console.log(carj2[0].NumberOfDoors?.CurrentTextValue || "i dont have doors");<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
“in”运算符可用于检查对象是否包含某个键。那个?运算符还可用于确保JavaScript不会尝试获取和使用未定义的值。
https://stackoverflow.com/questions/62595448
复制相似问题