TypeError:比较数组时,无法读取未定义的属性“0”?
我有一个具有JSON值的数组
category = [ {"categoryName":"abcd","productName":"chicken biriyani","price":100,"quantity":"medium"}, {"categoryName":"biriyani","productName":"mutton biriyani","price":130,"quantity":"medium"} ]
我必须检查上述类别数组所需的参数。 所以我创build了一个必需的参数数组。 var reqarray = ['categoryName','productName','price','quantity'];
我创build了用于检查reqarray的值和类别的每个值的示例代码,但它不工作
var categoryList = [ {"categoryName":"abcd","productName":"chicken biriyani","price":100,"quantity":"medium"}, {"categoryName":"biriyani","productName":"mutton biriyani","price":130,"quantity":"medium"} ]; for(var i in categoryList){ var reqarray =['categoryName','productName','price','quantity']; for(var j in arr){ if(categoryList[i].reqarray[j]=='null' || categoryList[i].reqarray[j] == " "){ console.log("data is null"); } else if(categoryList[i].reqarray[j] == undefined){ console.log("required parameters"); } else{ console.log("ok"); } } } }
此代码显示一个错误:
TypeError: Cannot read property '0' of undefined
点符号不能用于你的代码,因为你在reqarray中有string。 string可用于使用括号表示访问对象的成员。
例如:
var obj ={ name: 'x'' age: 22 };
现在你不能使用obj.'name来访问obj的name属性,但可以使用obj ['name']来实现。
因此,只要将categoryList [i] .reqarray [j]更改为categoryList [i] [reqarray [j]],那么您应该很好。
所以你的代码应该是:
var categoryList = [ {"categoryName":"abcd","productName":"chicken biriyani","price":100,"quantity":"medium"}, {"categoryName":"biriyani","productName":"mutton biriyani","price":130,"quantity":"medium"} ]; for(var i in categoryList){ var reqarray =['categoryName','productName','price','quantity']; for(var j in reqarray){ if(categoryList[i][reqarray[j]]=='null' || categoryList[i][reqarray[j]] == " "){ console.log("data is null"); } else if(categoryList[i][reqarray[j]] == undefined){ console.log("required parameters"); } else{ console.log("ok"); } } }
希望能帮助到你!!
人们可以把一个对象看作是一个关联数组(也就是map,dictionary,hash,lookup table)。 该数组中的键是对象属性的名称。
有两种访问属性的方法:点符号和括号符号。
点符号:
get = object.property; object.property = set;
使用点符号的方法调用:
document.createElement('pre');
括号表示法
get = object[property_name]; object[property_name] = set;
使用括号表示的方法调用:
document['createElement']('pre');
在这种情况下,文字'createElement'
是已知的,您可以使用点运算符直接访问该属性。 如果createElement
是expression式求值的结果,则需要使用括号表示法,从而指示JS编译器首先评估括号内的内容,然后访问对象的求值属性处的值。
var categoryList = [ {"categoryName":"abcd","productName":"chicken biriyani","price":100,"quantity":"medium"}, {"categoryName":"biriyani","productName":"mutton biriyani","price":130,"quantity":"medium"} ]; for(var i in categoryList){ var reqarray =['categoryName','productName','price','quantity']; for(var j in reqarray){ if(categoryList[i][reqarray[j]]=='null' || categoryList[i][reqarray[j]] == " "){ console.log("data is null"); } else if(categoryList[i][reqarray[j]] == undefined){ console.log("required parameters"); } else{ console.log("ok"); } } }
假设你的意思是reqarray
,你只需categoryList[i].reqarray[j]
categoryList[i][reqarray[j]]
来replacecategoryList[i].reqarray[j]
categoryList[i][reqarray[j]]
。
另外作为一个方面说明,你不应该使用for..in
数组。 它的意思是循环对象。 如果你有数组,使用或array
函数。
样品
var categoryList = [{ "categoryName": "abcd", "productName": "chicken biriyani", "price": 100, "quantity": "medium" }, { "categoryName": "biriyani", "productName": "mutton biriyani", "price": 130, "quantity": "medium" }]; var reqarray = ['categoryName', 'productName', 'price', 'quantity']; categoryList.forEach(function(c) { reqarray.forEach(function(k) { if (c[k] == 'null' || c[k] == " ") { console.log("data is null"); } else if (c[k] == undefined) { console.log("required parameters"); } else { console.log("ok"); } }) })