0

I am reading some data from the database which are numbers.

What I need to do is to assign a name to each number ...

something like:

get the variable: ... thevar = '1';

If thevar = 1 then myname = 'name1';
else
If thevar = 2 then myname = 'name2';

How can I do this with javascript?

Sorry, I don't think I've explained it properly...what I need if the syntax on how I would do this the above with javascript.

Hope it now makes more sense.

2
  • 4
    myname = 'name' + myvariable? Commented Aug 29, 2012 at 14:35
  • I still have no idea what the question is. Did ChaosPandion get it right? Commented Aug 29, 2012 at 14:39

3 Answers 3

3

You can concatenate numbers and strings in JavaScript:

myname = 'name' + myvariable
Sign up to request clarification or add additional context in comments.

Comments

1

How about myname = 'name' + myvariable;

Assuming you want the syntax of w.e you have posted,

var thevar = '1'; //read from database
var myname = '';

if (thevar === '1') {
  myname = 'name1';
} else if (thevar === '2') {
  myname = 'name2';
}

1 Comment

@Satch3000 Updated answer.. not sure if that is what you want.
0
function buildNumberMap(numbers) {
    var i, len, number, map = {};
    for (i = 0, len = numbers.length; i < len; i++) {
        number = numbers[i];
        map["name" + number] = number;
    }
    return map;
}

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.