TypeError: Cannot read property '0'比较数组时未定义的

TypeError: Cannot read property '0' of undefined when comparing the arrays?

本文关键字:数组 未定义 比较 property Cannot TypeError read      更新时间:2023-09-26

我有一个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。它的意思是循环遍历对象。如果您有数组,请使用forarray函数。

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");
        }
    }
}

希望有帮助!!