TypeError: Cannot read property '0'比较数组时未定义的
TypeError: Cannot read property '0' of undefined when comparing the arrays?
我有一个JSON值数组
category = [ {"categoryName":"abcd","productName":"chicken biriyani","price":100,"quantity":"medium"}, {"categoryName":"biriyani","productName":"mutton biriyani","price":130,"quantity":"medium"} ]
我必须检查上述类别数组所需的参数。所以我创建了一个必需的参数数组。var reqarray = ['categoryName','productName','price','quantity'];
我创建了用于检查reqarray的值和category的每个值的示例代码。但它不工作
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
可以把对象看作一个关联数组(又名map、字典、散列、查找表)。这个数组中的键是对象的属性名。
有两种访问属性的方法:点表示法和括号表示法。
点符号:
get = object.property;
object.property = set;
使用点符号的方法调用:
document.createElement('pre');
括号
get = object[property_name];
object[property_name] = set;
方法调用使用括号表示法:
document['createElement']('pre');
在这种情况下,文字'createElement'
是已知的,您可以使用点操作符直接访问该属性。在createElement
是表达式求值的结果的情况下,那么你需要使用括号符号,从而指示JS编译器首先求括号内的内容,然后访问对象的evaluate 属性的值。
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");
}
}
}
假设arr
指的是reqarray
,则只需将categoryList[i].reqarray[j]
替换为categoryList[i][reqarray[j]]
即可。
还需要注意的是,不应该在数组上使用for..in
。它的意思是循环遍历对象。如果您有数组,请使用for
或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");
}
})
})
点符号不能在你的代码中工作,因为你在reqarray中有字符串。字符串可用于使用括号表示法访问对象的成员。
,
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");
}
}
}
希望有帮助!!
- 用HTML5FileReader填充的Javascript多维数组未定义(但仅在首次使用时!)
- 访问节点js中对象内部的数组-未定义
- 嵌套的“for”循环 - 数组未定义
- Javascript 数组未定义的值
- 我的 JS 数组未定义 x 7 的问题
- JSON 多维数组未显示(“数组未定义”)
- 挖空嵌套模型可观察数组未定义
- 嵌套对象,访问父对象中的对象数组 = 未定义
- 对象的Javascript数组未定义
- Javascript数组未定义..并且I'I’我不知道为什么
- json对象数组未定义错误
- Javascript数组未定义,没有大小
- Javascript全局数组未定义
- JavaScript二维数组未定义
- 控制台显示数组未定义
- $ajax请求不能保存到数组(未定义)
- 为什么我的数组“未定义”?
- JSON数组未定义
- 类型错误:数组未定义
- JSON数据到Javascript数组未定义