Title: Accessing variable inside array of objects JSON
Tags: javascript;json
Question: 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;
```

Comment: Did you try var photo   = this.directory + this.photos[this.currentPhoto].photor; that is without [0]??

Comment: Can you send complete code in jsfiddle? Make sure 'this.currentPhoto' returns you index of current photo and try my code.

Comment: `var photo   = this.directory + this.photos[this.currentPhoto].photor;
            var caption = this.captions[this.currentPhoto].captionr;`

Comment: yes, it says "file not found" then, even though files are there

Comment: http://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

Here is the accepted answer: Try this 

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


Here is another answer: 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 };
        };
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:09/29/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><json>', 'Title': 'Accessing variable inside array of objects JSON', 'ViewCount': 61, 'id': 32837653, 'user_AboutMe': None, 'user_AccountId': 5311345.0, 'user_DisplayName': 'Nebular Dust', 'user_Reputation': 393.0, 'user_Views': 53.0, 'user_id': 4237539.0}}