0

I'm really not sure how to explain what i'm trying to achieve, but what is the easiest way to do something like this (in javascript):

function switchItUp(i){
    ...
}

old_values = [1, 2, 3, 4];
new_values = ['a', 'b', 'c', 'd'];

switchItUp(3); // returns "c"

2 Answers 2

1
function switchItUp(i) {
    return new_values[old_values.indexOf(i)];
}

Keep in mind that the indexOf function does not work in IE8 and earlier. If you want support in those browsers, you may want to try jQuery's inArray function or Underscore.js's indexOf function.

Sign up to request clarification or add additional context in comments.

1 Comment

Works... depending on the browser. indexOf for arrays has limited support.
0

Loop through first array until value is found, use looping variable to index into second array:

function switchItUp(i) {
    var j, l = old_values.length;
    for (j = 0; j < l; j += 1) {
        if (old_values[j] === i) {
            return new_values[j];
        }
    }
}

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.