为什么我的JavaScript对象会抛出一个未定义的错误?

时间:2014-03-13 02:43:51

标签: javascript arrays object multidimensional-array

我在控制台中收到此错误:

Uncaught TypeError: Cannot read property 'kbsrc' of undefined

我收到此错误,因为我认为我创建JavaScript对象的方式可能有问题。我从来没有创建过像这样多维的对象,所以我可能在某个地方搞砸了语法。我在这里生成标记文件中的对象:

var kb_work = {}
    kb_work['2014'] = {};
        kb_work['2014']['kbid'] = ["51","47"];
        kb_work['2014']['kbsrc'] = ["images\/images-4.jpeg","images\/imgres-3.jpeg"];
        kb_work['2014']['kbtitle'] = ["shalom","Test 6"];
        kb_work['2014']['kbmedium'] = ["2x2","Oil"];
        kb_work['2014']['kbsize'] = ["2x2","2x6"];
        kb_work['2014']['kbdate'] = ["2014","2014"];
    kb_work['2013'] = {};
        kb_work['2013']['kbid'] = ["55","54","53","52","50"];
        kb_work['2013']['kbsrc'] = ["images\/imgres-4.jpeg","images\/imgres-3.jpeg","images\/imgres-1.jpeg","images\/images.jpeg","images\/images-3.jpeg"];
        kb_work['2013']['kbtitle'] = ["totally","heheh","Howdy","tickle","hi"];
        kb_work['2013']['kbmedium'] = ["oil","oil","2x2","o","oil"];
        kb_work['2013']['kbsize'] = ["2x2","2x2","2x2","2x2","2x1"];
        kb_work['2013']['kbdate'] = ["2013","2013","2013","2013","2013"];
    kb_work['2012'] = {};
        kb_work['2012']['kbid'] = ["49"];
        kb_work['2012']['kbsrc'] = ["images\/images-2.jpeg"];
        kb_work['2012']['kbtitle'] = ["chicked"];
        kb_work['2012']['kbmedium'] = ["oil"];
        kb_work['2012']['kbsize'] = ["3x4"];
        kb_work['2012']['kbdate'] = ["2012"];

这些数组中的每一个目前只有一个值,但会随着用户添加工作而增长。在此之后,我链接到一个文件,其中包含TypeError引用的此函数(我对特定行进行了评论):

function changeGal(gallery_year) {
  $("#gallery-control-bar").fadeOut(t);
  $("#gallery-image").fadeOut(t);
  $("#info").fadeOut(t);
  $("#gallery-viewer").fadeOut(t);

  //this is where the script chokes up referring to "currentImg" which is 0 and refers to the first value in the array "['2014']['kbsrc']".

    $("#gallery-image").html("<img src='" + kb_work[gallery_year]['kbsrc'][currentImg] + "'>");
    $("#gallery-title").html(kb_work[gallery_year]['kbtitle'][currentImg]);
    $("#gallery-medium").html(kb_work[gallery_year]['kbmedium'][currentImg]);
    $("#gallery-size").html(kb_work[gallery_year]['kbsize'][currentImg]);
    $("#gallery-date").html(kb_work[gallery_year]['kbdate'][currentImg]);

  $("#gallery-control-bar").delay(t + d).fadeIn(t);
  $("#gallery-image").delay(t + d).fadeIn(t);
  $("#info").delay(t + d).fadeIn(t);

  var userCurrent = currentImg + 1;
  var userTotal = kb_work[gallery_year][0].length;

  $("#current-post").html(userCurrent);
  $("#post-total").html(userTotal);

  var galWidth = $("#gallery-image" > "img").width();
  $("#gallery").width(galWidth);

}

为什么它不能参考价值的任何想法?

2 个答案:

答案 0 :(得分:2)

我认为你需要

$("#gallery-image").html("<img src='" + kb_work[gallery_year][gallery_year + '.kbsrc'][currentImg] + "'>");

因为gallery_year看起来像2013年的年份值,但关键是像2013.kbsrc这样的串联字符串值

您的结构还有另一个问题,因为kb_work[year]应该是一个对象而不是一个数组,再次第二级别的键不需要再次使用年份。

因此结构可以更新为

var kb_work = {}
kb_work['2014'] = {};
kb_work['2014']['kbid'] = ["46"];
kb_work['2014']['kbsrc'] = ["images\/screen shot 2014-03-05 at 11.31.04 pm.png"];
kb_work['2014']['kbtitle'] = ["Test 5"];
kb_work['2014']['kbmedium'] = ["Oil"];
kb_work['2014']['kbsize'] = ["2x5"];
kb_work['2014']['kbdate'] = ["2014"];
kb_work['2013'] = {};
kb_work['2013']['kbid'] = ["44"];
kb_work['2013']['kbsrc'] = ["images\/screen shot 2014-03-05 at 11.31.04 pm.png"];
kb_work['2013']['kbtitle'] = ["Test 3"];
kb_work['2013']['kbmedium'] = ["Oil"];
kb_work['2013']['kbsize'] = ["2x1"];
kb_work['2013']['kbdate'] = ["2013"];
kb_work['2012'] = {};
kb_work['2012']['kbid'] = ["45"];
kb_work['2012']['kbsrc'] = ["images\/screen shot 2014-03-05 at 11.31.04 pm.png"];
kb_work['2012']['kbtitle'] = ["Test 4"];
kb_work['2012']['kbmedium'] = ["Oil"];
kb_work['2012']['kbsize'] = ["2x3"];
kb_work['2012']['kbdate'] = ["2012"];

然后访问它

kb_work[gallery_year]['kbsrc'][currentImg]

答案 1 :(得分:0)

您的嵌套显示您认为这与其实际工作方式不同...

var kb_work = {}
    kb_work['2014'] = new Array();
        kb_work['2014.kbid'] = ["46"];

实际上会产生如下对象:{"2014":[],"2014.kbid":["46"]}

我相信你想要这个:

var kb_work = {
    '2014': [
        { 'kbid': ["46"] }
     ]
};

这导致像这样的对象:{"2014":[{"kbid":["46"]}]}

现在您可以访问:

kb_work['2014'][0].kbid[0] // 46

你可以将年份作为包含字符串的变量传入,0 s可以是包含索引的变量。

您可以添加多行,如下所示:

var kb_work = {
    '2014': [
        { 'kbid': ["46"] },
        { 'kbid': ["62"] },
        { 'kbid': ["90"] }
     ]
};