-1

This is a sample of my Javascript code below. The main reason why I am writing this post is because I have a question about the last function, which is my GalleryImage function. There are four string variables inside that function. In each variable, I am supposed to store an element from the correspoding key in my JSON file's array. How do I do that? Underneath my javascript code is a sample of my JSON file code.

function GalleryImage(mJSON) {

    var locate = "";
    var description = "";
    var date = "";
    var url = "";
    //implement me as an object to hold the following data about an image:
    //1. location where photo was taken
    //2. description of photo
    //3. the date when the photo was taken
    //4. either a String (src URL) or an an HTMLImageObject (bitmap of the photo. https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement)
}

"images": [
        {
            "imgPath": "img/places/australia.jpg",
            "imgLocation": "Australia",
            "description": "Loch Ard Gorge",
            "date": "01/01/2016"
        },
        {
            "imgPath": "img/places/austria.jpg",
            "imgLocation": "Austria",
            "description": "Austrian chapel",
            "date": "01/02/2016"
        }

  ]
}
  • @Biffen i edited it – italkapple Apr 02 '18 at 14:06
  • 3
    JSON is a notation format. If you do `var images = [` you will have a JS object. I assume that is what you really mean. Please create a [mcve] to explain the input and expected output – mplungjan Apr 02 '18 at 14:08
  • @mplungjan Starting from that var images code is my JSON file, so it is JSON code. I just need to some how get each key from the images array into the variables inside the Javascript function I wrote. The variables will be strings. – italkapple Apr 02 '18 at 14:11
  • It is useless as "JSON code" You will get a JS object or you need to use JSON.parse to make the string a JS object. – mplungjan Apr 02 '18 at 14:45
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Biffen Apr 02 '18 at 15:15

2 Answers2

-1

You can access the variables from the JS object array like you would do it in a dictionary:

element.variable

Example:

mJson.description

I hope my answer was helpful!

mplungjan
  • 169,008
  • 28
  • 173
  • 236
MarvMan
  • 41
  • 9
-1

You can try this one:

debugger;
const images = [
        {
            "imgPath": "img/places/australia.jpg",
            "imgLocation": "Australia",
            "description": "Loch Ard Gorge",
            "date": "01/01/2016"
        },
        {
            "imgPath": "img/places/austria.jpg",
            "imgLocation": "Austria",
            "description": "Austrian chapel",
            "date": "01/02/2016"
        }
];

const GalleryImage = (props) => {
    return {
     locate : props.imgLocation,
     description :  props.description,
     date :  props.date,
     url :  props.imgPath ? props.imgPath : props.imgPath // Here is where you need to check if is a string or use the default bitmap
    };
}

const imagesParsed = images.map(props => GalleryImage(props));

Another option is trying to do it with a class but is basically the same concept.

Miguel Angel
  • 944
  • 8
  • 20