访问对象数组JSON中的变量

时间:2015-09-29 07:01:48

标签: javascript json

我试图访问" photor"和#34;字幕"存储在该JSON数组中的每个对象的数量。但它不起作用并给我一个错误"无法读取属性' photor'未定义"

var slideshow = {

    directory: "images/",

    photos:[
            { "photor": "aurelius.jpg", "captionr" : "Mark Aurelius"}, 
            { "photor": "cesar.png", "captionr" : "Gaius Julius Ceasar"},
            { "photor": "couple.jpg", "captionr" : "Greek Couple"},
            { "photor": "flavian.jpg", "captionr" : "Flavian Woman"},
            { "photor": "lucius.jpg", "captionr" : "Lucius Verus"},
            { "photor": "lupe.jpg", "captionr" : "Emperor Caracalla"},
            { "photor": "sabina.jpg", "captionr" : "Sabina"}
            ],

    currentPhoto: 0,

    getPrevious: function(){
            if (this.currentPhoto == 0) 
                this.currentPhoto = this.photos.length-1;
            else
                this.currentPhoto--;

            var photo   = this.directory + this.photos[this.currentPhoto][0].photor;
            var caption = this.photos[this.currentPhoto][1].captionr;
            return { "photo": photo, "caption": caption };
        };

         var photo=this.directory+this.photos[this.currentPhoto].photor;
    var caption=this.photos[this.currentPhoto].captionr;

2 个答案:

答案 0 :(得分:1)

试试这个

var photo = this.directory + this.photos[this.currentPhoto].photor;

答案 1 :(得分:0)

你可能正在努力解决JS

这个的行为

试试这个 -

getPrevious: function(){
            var ref=this;
            if (ref.currentPhoto == 0) 
                ref.currentPhoto = ref.photos.length-1;
            else
                ref.currentPhoto--;

            var photo   = ref.directory + ref.photos[ref.currentPhoto][0].photor;
            var caption = ref.captions[ref.currentPhoto][1].captionr;
            return { "photo": photo, "caption": caption };
        };