Errol Fitzgerald
Errol Fitzgerald

Reputation: 3008

How do I use a string value to access an array in javascript?

This has been bugging me for quite some time now. All I want to do is return a value from an array given its key, but use a variable (_interview) to choose what array to return from.

var utils = function () {

var _language = "";
var _interview = "";
var _base = "";

var _alerts = function (code) {

    var en = new Array(),
        fr = new Array();

    en[0] = "You must enter a value in the box before continuing.";
    fr[0] = "Vous devez entrer une sélection pour les options mises en évidence.";

    // I know _interview is returning the correct value of either "en" or "fr"
    // I have tried this[_interview][code] but I get property not defined at the index
    // How do I return the index from the right array?
};

return {

    lang : function () {

        _language = $("#language option:selected").val();
        var answer = confirm(_alerts(0)); // Call from here

    }
};
}();

$(document).ready(function ()
{
    utils.lang();
});

Upvotes: 1

Views: 98

Answers (4)

Matt Ball
Matt Ball

Reputation: 360046

Bracket notation is the right way to do it. However, you need to put the languages into an array, since this does not refer to the function scope as you seem to think. In this eedparticular case, this === window which is obviously not what you want!

Give this a shot instead:

var utils = function() {

    var _language = "";
    var _interview = "";
    var _base = "";

    var _alerts = function(code) {

        var messages = {
            en: ["You must enter a value in the box before continuing."],
            fr: ["Vous devez entrer une sélection pour les options mises en évidence."]
        };

        // I know _interview is returning the correct value of either "en" or "fr"
        // I have tried this[_interview][code] but I get property not defined at the index
        // How do I return the index from the right array?
        return messages[_language][code];
    };

    return {

        lang: function() {

            _language = $("#language").val(); // note the selector change
            var answer = confirm(_alerts(0)); // Call from here
        }
    };
}();

$(document).ready(function() {
    utils.lang();
});​

Working demos: english|french

Upvotes: 1

Marek Sebera
Marek Sebera

Reputation: 40681

Better to use 2D Array (deep object structure), and strings as keys instead of integer indexes

var languages = {
    'en': {
        'string_key_1' : 'String For Key 1'
    },
    'fr': {
        'string_key_1' : 'Le String ...'
    }
};

To get values like

var key = 'string_key_1';
var translated_value = languages[_interview][key];

Upvotes: 3

rjz
rjz

Reputation: 16510

You can access the value of an object property using square brackets, like so:

var index = 'foo',
    myObj = {
      foo: 'bar'
    };

alert(myObj[index]); // 'bar'

The same goes for arrays, but only numeric keys will work:

var index = 1,
    myArr = ['foo','bar'];

alert(myArr[index]); // 'bar'

Upvotes: 1

Kory Sharp
Kory Sharp

Reputation: 490

Check out the eval() function. This should help you accomplish what you need.

Upvotes: -1

Related Questions