1

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

1
  • Either you can use array or index based object to map file data; Commented Nov 26, 2016 at 13:46

3 Answers 3

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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"];

Comments

1
var obj = {};


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

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

//0.json, 1.json

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.