admin管理员组

文章数量:1426953

I have an array ARRAY whose length is dynamic. In the below example it is 5 but it may be 10 or 15 also

ARRAY = [A,B,C,D,E];
var mlength = ARRAY.length;
Using this mlength, how can I create variables. For example  

I want to assign as

 mname0=ARRAY[0]; mname1 = ARRAY[1]; mname2= ARRAY[2]; mname3 = ARRAY[3]; mname4 = ARRAY[4];

I have tried the below code. But that's creating reference error Invalid left-hand side in assignment

 for (var i = 0, mlength = ARRAY.length; i < mlength; i++) {
     'mname'+i = ARRAY[i];
 }

How can I create dynamic variables?

I have an array ARRAY whose length is dynamic. In the below example it is 5 but it may be 10 or 15 also

ARRAY = [A,B,C,D,E];
var mlength = ARRAY.length;
Using this mlength, how can I create variables. For example  

I want to assign as

 mname0=ARRAY[0]; mname1 = ARRAY[1]; mname2= ARRAY[2]; mname3 = ARRAY[3]; mname4 = ARRAY[4];

I have tried the below code. But that's creating reference error Invalid left-hand side in assignment

 for (var i = 0, mlength = ARRAY.length; i < mlength; i++) {
     'mname'+i = ARRAY[i];
 }

How can I create dynamic variables?

Share Improve this question asked Mar 23, 2015 at 12:48 rji rjirji rji 7073 gold badges17 silver badges37 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 1

variables in the global scope could also be considered as members of the window-object:

var mname,ARRAY = ["A","B","C","D","E"];
var mlength = ARRAY.length;
for(var i=0;i<mlength;i++){
    window["mname"+i]=ARRAY[i];
}
alert(mname0);

But you should consider working directly with ARRAY instead

Change to this:

var ARRAY = ['A', 'B', 'C', 'D', 'E'];
var mlength = ARRAY.length;


for (var i = 0; i < mlength; i++) {
  console.log('mname' + i + ' = ' + ARRAY[i]);
}

This answer is taken from this question which I am the author. Yet I don't feel the question is a duplicate itself.


To create dynamic variable name, here 3 options.

eval (not remended)

You can use the Javascript function eval to achieve what you want. But be aware, this in not remended (I emphasized it twice, hope you understood!).

Don't use eval needlessly!

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.

It would be used like that :

eval('var mname' + i + ' = "something";');

Window object notation (Better than eval, still not really remended)

This method consist of using the object notation provided by JavaScript on the global window object. This is polluting the global window scope and can be overrided by other JS files which is bad. If you want to know more on that subject, this is a good question : Storing a variable in the JavaScript 'window' object is a proper way to use that object?.

To use that method, you would do thing like that :

window['mname' + i] = something;
alert(window[varName]);

Using an object (remended)

The best solution would be to creating you own variable scope. For instance, you could create on the global scope a variable and assign an object to it. You can then use the object notation to create you dynamics variables. It work the same way as the window does :

var globalScope = {};

function awesome(){
    var localScope = {};
    globalScope['mname' + i] = 'something';
    localScope['mname' + i] = 'else';

    notSoAwesome();
}

function notSoAwesome(){
    alert(globalScope['mname' + i]); // 'something';
    alert(localScope['mname' + i]); // undefined
}

Try this,

var arr= [];
for(var i =0; i <15; i++) {
  arr.push[{'mname'+i: 'ARRAY'+i}];
}

Here is the link where i have generated array

$(document).ready(function (e){  
     var cars = ["Saab", "Volvo", "BMW"];
     var text=''; 
     var mname="name";

     for (i = 0; i < cars.length; i++) { 
        text += mname+i+'='+cars[i]+',';
     }
     alert(text);
});

where your increment value based on your requirement it will generated string dynamically. I hope this will help you.

You can do something like this.

var mname;
var ARRAY = ['A','B','C','D','E'];
var mlength = ARRAY.length;
for(var i=0;i<mlength;i++){
   window['mname'+i]=ARRAY[i];
}
console.log(mname1);

So this might help you. In this code I am making all the variables as window object member since Window is Global Object.

本文标签: