3

I have value an array like this:

multi_arr = ["resi_spec","resi_desc"];

So each array value is considered as a variable and I want to store some value of these variables dynamically like this:

resi_spec = "good morning"
resi_desc = "good evening";

So that the array values are converted as variables. Is this possible?

I don't want use use obj[resi_spec] like this and i used array not variable if i just enter resi_spec means , i'll get good morning.

4
  • Where do you get the values that should fit in ? Commented Sep 14, 2015 at 13:03
  • 1
    This sounds like something you should be using an object for. Commented Sep 14, 2015 at 13:05
  • Where do you get the values "good morning" and ""good evening"? Commented Sep 14, 2015 at 13:06
  • The current upvoted answers works. The gist is 1) you need a container object (be it global or window); 2) you can use variable in bracket notation. Commented Sep 14, 2015 at 13:12

4 Answers 4

6

You could use an object to hold the values:

var multi_arr = ["resi_spec","resi_desc"];
var resi = {};
multi_arr.forEach(function(val) { 
    resi[val] = "good morning"; 
});

Obviously you will want to set different values for each object attribute which you could accomplish with a helper function:

var lookupHelper = function(key) {
    if (key == 'resi_spec') return 'good morning';
    if (key == 'resi_desc') return 'good evening';
};
var multi_arr = ["resi_spec","resi_desc"];
var resi = {};
multi_arr.forEach(function(val) { 
    resi[val] = lookupHelper(val); 
});

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

2 Comments

Good approach, although the forEach method is going to be quite useless if you want to have different values. However you could use another array for those…
To set different values, you would just implement a bit of additional code to look up the proper value. I've added an example to show how you would accomplish this.
4

You could also do the following.. Although not really recommended, since you will be creating global variables. Jsfiddle

var varNames = ["name1", "name2", "name3"];
for(var i = 0; i < varNames.length; i++){
  window[varNames[i]] = i;
}
console.log(name1); // 0
console.log(name2); // 1
console.log(name3); // 2

2 Comments

This is basically almost the same as the object approach only that your object is the global object.
@Xufox but unlike other answers you can access them with their name directly (even not recommended)
4

The best way is having an object like bellow

multi_arr = ["resi_spec","resi_desc"];
var obj = {};
obj[multi_arr[0]] = "good morning";
obj[multi_arr[1]] = "good evening";
console.log(obj); //prints Object {resi_spec: "good morning", resi_desc: "good evening"})

you can access variables like bellow

console.log(obj["resi_spec"]); //prints "good morning";
console.log(obj["resi_desc"]); //prints "good evening";

2 Comments

Sorry This is not what i expected. just i enter alert(resi_spc); i want to get answer good morning. i won't use object is it possible?
@SatheeshSivaNallathambi what do you want then ?
-1

Okay, so I misunderstood your question, so here is my edited response.

var multi_arr = ["resi_spec","resi_desc"];

var object = {};

object[multi_arr[0]] = "good morning";
object[multi_arr[1]] = "good evening";

console.log(object);

2 Comments

You’d be better off just deleting your answer and posting a new one, although now it’s probably too late.
There is little point reposting as other answers are now valid.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.