Frage

I would like to dynamically generate an integer (in the below case either 100 or 500) and use it to access separate arrays. In a later step (not part of the code below) I would also like to access different parts of those arrays in the same manner ("message 1, 2 or 3").

For this proof of concept I did not dynamically generate an integer, but I just set it to 100.

I then tried to use eval() to dynamically generate the array name consisting of "warning" and 100, but it is not working properly.

This is my code:

// two arrays are defined, warning100 and warning500
var warning100 = [
    { "message1":"Ok, go ahead and start typing!" },
    { "message2":"Keep going!" },
    { "message3":"You can do it!" }
];

var warning500 = [
    { "message1":"Slow down..." },
    { "message2":"That's it!" },
    { "message3":"Maximum reached." }
];

// set i to 100 and h to 1 for testing purposes, will be random integers in the final version
var i = 100;
var h = 2;
// create variable names as a combination of a string and i or h
// those variables will be used to access one of the arrays from above and one of the messages;
eval("var warningNumber = warning" + i + ";");
eval("var messageNumber = message" + h + ";");

/* alternative code for creating the two variable values
var warningNumber = "warning" + i;
var messageNumber = "message" + h;
*/

// the variable warningNumber from above is now used again to access the array warning100
// the varaible messageNumber is used to access one of the messages
// within that array message1 should be displayed
// create variable to be used in the document.write below
var warning = warningNumber[0].messageNumber;

// should alert "Ok, go ahead and start typing!"    
alert(warning);
War es hilfreich?

Lösung

Why not just make each set of warnings part of a warningNumber object? That way you could do

var warnings = {100: { 1:"Ok, go ahead and start typing!",
                       2:"Keep going!",
                       3:"You can do it!"
                     },
                500: { 1:"Slow down...",
                       2:"That's it!",
                       3:"Maximum reached."
                     }
               };
alert(warnings[i][h]);

That way all the eval doesn't even has to be done.

Andere Tipps

Why not just wrap it in an object?

var dynamicHolder = {};
var i = 100;
var h = 2;
dynamicHolder["warningNumber"] = "warning" + i;
dynamicHolder["messageNumber"] = "message" + h;

Try this:

var warnings = {

    warning100: {
        "message1":"Ok, go ahead and start typing!",
        "message2":"Keep going!",
        "message3":"You can do it!"
    },
    warning500: {
        "message1":"Slow down...",
        "message2":"That's it!",
        "message3":"Maximum reached."
    }
}

var i = 100;
console.log( warnings["warning" + i] );
i += 400;
console.log( warnings["warning" + i] );

console.log( warnings["warning" + i ]["message1"] );

Example

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top