ogot
ogot

Reputation: 341

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?

Thanks

Upvotes: 1

Views: 94

Answers (3)

Harminder
Harminder

Reputation: 141

var obj = {};


for (i = 0; i<10; i++)
{

obj[i] = i + ".json";
}
console.log(obj[0], obj[1]);

//0.json, 1.json

Upvotes: 1

marcan2020
marcan2020

Reputation: 609

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

Upvotes: 2

Eran Shabi
Eran Shabi

Reputation: 14979

No magic solution. You must get the files connected to an index in some format (for example ["1.json", "2.json"]). You can put this information in another external file and use it, or you can save an network request by making it hard coded in your code, like this:

var files = ["1.json", "2.json"];

Upvotes: 1

Related Questions