1

I'm trying to access "photor" and "captionr" of each object stored in that JSON array. But it doesn't work and gives me an error "Cannot read property 'photor' of undefined"

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;
5
  • Did you try var photo = this.directory + this.photos[this.currentPhoto].photor; that is without [0]?? Commented Sep 29, 2015 at 7:05
  • yes, it says "file not found" then, even though files are there Commented Sep 29, 2015 at 7:07
  • var photo = this.directory + this.photos[this.currentPhoto].photor; var caption = this.captions[this.currentPhoto].captionr; Commented Sep 29, 2015 at 7:08
  • Can you send complete code in jsfiddle? Make sure 'this.currentPhoto' returns you index of current photo and try my code. Commented Sep 29, 2015 at 7:12
  • jsfiddle.net/8mgb3m1c it's strange as I have all those files in "images" folder and the one that I put in html works showing the first image, but it keep giving me "file not found" error Commented Sep 29, 2015 at 7:14

2 Answers 2

1

Try this

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

Sign up to request clarification or add additional context in comments.

Comments

0

Probably , you are struggling with behaviour of this in JS

Try this -

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.