【问题标题】:TypeError: Cannot read property '0' of undefined when comparing the arrays?TypeError:比较数组时无法读取未定义的属性“0”?
【发布时间】:2016-10-19 19:13:09
【问题描述】:

我有一个包含 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

【问题讨论】:

  • 什么是arr?我猜categoryList[i].reqarray[j] 应该是categoryList[i][reqarray[j]]

标签: javascript arrays node.js for-loop express


【解决方案1】:

可以将对象视为关联数组(也称为映射、字典、哈希、查找表)。此数组中的键是对象属性的名称。

访问属性有两种方式:点表示法和方括号表示法。

点符号:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      点符号不适用于您的代码,因为您在 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");
              }
          }
      }
      

      希望对你有帮助!!

      【讨论】:

      • 我可以使用obj.name 来访问对象值。
      • @arnabkaycee obj.name 可以工作,但 obj.'name' 不行
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 2019-08-04
      • 2015-06-08
      相关资源
      最近更新 更多