ogot ogot - 8 months ago 56
Javascript Question

Load JSON file by object value

I have a slide interface in jQuery that loads a JSON file on each new slide. The JSON files are named with sequential numeric values (1.json, 2.json..) to determine the order of the slides. I would like this to be a value in the object ("index-position") rather than the name of the file.

My question is how would I load these files sequentially in the same manner - but rather than load the order by file name, load them by index position?

My hunch is that this means creating an array of 'index-position' values of all the files and associating each index to a file name, but perhaps there is a simpler/more efficient method?


Answer Source

You can read the content of the json before (read the "index-position") and set it to your global array.

Something like:

var slides = [];
$.getJSON( "ajax/fileName.json", function( data ) {
    var index = data["index-position"];
    slides[index] = data;