无法读取属性'0'的未定义 [英] Cannot read property '0' of undefined

查看:747
本文介绍了无法读取属性'0'的未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

getBrand: function(brandId) {
  for(var i=0; i<malls.length; i++) {
    for(var j=0;j<malls[i].brands.length; j++){
      //console.log(malls[i][brands[j]);
      //console.log(malls[i].brands[j].id);
      if(malls.brands[j].id==(brandId)){
        console.log(malls.brands[j].id);
        return malls.brands[j].id;
      }
    }
  }
}

我需要帮助,请和我的控制台我得到这个消息:类型错误:无法读取属性'0'未定义

I need help please, and in my console i get this message:TypeError: Cannot read property '0' of undefined

推荐答案

看来你有对象的数组,每个对象都有一个品牌属性,它包含数组对象。你的第二个循环假定商场阵列本身具有品牌财产是不正确的。由于 malls.brands 未定义你会得到错误。

It seems you have an array of objects and each object has a brands property that contains an array of objects. Your second for loop assumes the malls array itself has brands property which is not true. As the malls.brands is undefined you get that error.

修改

if (malls.brands[j].id==(brandId)) {

if (malls[i].brands[j].id==(brandId)) {

这篇关于无法读取属性'0'的未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆