如果细节包含文本,请在jQuery中创建一个我想要处理的对象。搜索周围但没有任何作用,例如这不起作用:
if(typeof data.details[0].text !== "undefined") {JScode}
格式1(json):
data = {kod: 4007, text: "Test.", details: []}
格式2(json):
data = {kod: 9004, text: "Test.", details: [{code: 9999, text: "Error"}]}
答案 0 :(得分:1)
您必须先检查是否details[0]
,然后才能检查details[0].text
是否未定义。
以下是一些片段:
let obj = {kod: 4007, text: "Test.", details: []}
if (typeof obj.details[0] !== "undefined" && typeof obj.details[0].text !== "undefined") {
console.log("Defined");
} else {
console.log("Undefined");
}
let obj = {kod: 9004, text: "Test.", details: [{code: 9999, text: "Error"}]}
if (typeof obj.details[0] !== "undefined" && typeof obj.details[0].text !== "undefined") {
console.log("Defined");
} else {
console.log("Undefined");
}
答案 1 :(得分:-1)
尝试使用.length
:
if (details[0].text.length > O) { it should exist }