user3095198
user3095198

Reputation: 83

Load a different js file with every load of the page

so basically I have a JS file, containing objects(var), and I want to be able to load a different Object every time I load the page. So for example, when the page is loaded, a new Objects 'temp' is created, that contains the data of some of the objects data* (data1, data2 etc.) from the JS file. I don't know how that works, and I don't know if I'm gonna need 5 .js files or one with 5 data variables.

    var data = {

    'coords' : [[784, 42], [802, 324], [128, 365], [710, 166], [513, 283], [610 , 391], [544, 48], [323, 204], [316, 50], [938, 52]],
    'walkthrough' : [8, 6, 0, 9, 3, 1, 5, 4, 2, 7],
    'highscore' : 2217  
}

var data1 = {
    'coords' : [[784, 42], [933, 211], [304, 190], [756, 264], [365, 395], [129, 302], [504, 261], [650, 393], [283, 26], [593, 35]],
    'walkthrough' : [8, 9, 0, 1, 3, 7, 6, 4, 5, 2],
    'highscore' : 2123

}

var data2 = {
    'coords' : [[784, 42], [591, 289], [242, 289], [710, 150], [465, 371], [589, 146], [667, 24], [323, 204], [173, 173], [898, 197] ],
    'walkthrough' : [8, 2, 7, 4, 1, 5, 3, 9, 0, 6],
    'highscore' : 1897

}

var data3 = {
    'coords' : [[785, 264], [591, 289], [392, 49], [419, 332], [862, 159], [533, 166], [710, 119], [323, 204], [600, 32], [834, 54]],
    'walkthrough' : [2, 7, 3, 1, 0, 4, 9, 6, 8, 5],
    'highscore' : 1573

}

var data4 = {
    'coords' : [[903, 292], [634, 352], [44, 350], [150, 188], [883, 131], [475, 240], [697, 152], [274, 341], [491, 69], [784, 34]],
    'walkthrough' : [2, 7, 5, 1, 0, 4, 9, 6, 8, 3],
    'highscore' : 2167

}

What I want is to have an object 'temp', that will have the data from some of the data objects.

Any help is appreciated, Thanks in advance.

Upvotes: 1

Views: 67

Answers (1)

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

This should do what you need...

var data = [{
    'coords' : [[784, 42], [802, 324], [128, 365], [710, 166], [513, 283], [610 , 391], [544, 48], [323, 204], [316, 50], [938, 52]],
    'walkthrough' : [8, 6, 0, 9, 3, 1, 5, 4, 2, 7],
    'highscore' : 2217  
}, {
    'coords' : [[784, 42], [933, 211], [304, 190], [756, 264], [365, 395], [129, 302], [504, 261], [650, 393], [283, 26], [593, 35]],
    'walkthrough' : [8, 9, 0, 1, 3, 7, 6, 4, 5, 2],
    'highscore' : 2123

}, {
    'coords' : [[784, 42], [591, 289], [242, 289], [710, 150], [465, 371], [589, 146], [667, 24], [323, 204], [173, 173], [898, 197] ],
    'walkthrough' : [8, 2, 7, 4, 1, 5, 3, 9, 0, 6],
    'highscore' : 1897

}, {
    'coords' : [[785, 264], [591, 289], [392, 49], [419, 332], [862, 159], [533, 166], [710, 119], [323, 204], [600, 32], [834, 54]],
    'walkthrough' : [2, 7, 3, 1, 0, 4, 9, 6, 8, 5],
    'highscore' : 1573

}, {
    'coords' : [[903, 292], [634, 352], [44, 350], [150, 188], [883, 131], [475, 240], [697, 152], [274, 341], [491, 69], [784, 34]],
    'walkthrough' : [2, 7, 5, 1, 0, 4, 9, 6, 8, 3],
    'highscore' : 2167
}];

var thisData = data[Math.floor(data.length * Math.random())];

Store all the data in one array, and then just select one of them randomly. It's a much better approach than having multiple js files as you'll only have one file to maintain.

Upvotes: 4

Related Questions