Splitting a rowset and storing in a dynamic variable

I get a set of values ​​as shown below from a line break

var values = [ "Budget1-green", "Team1-green", "Risk1-green", "Benefit1-green", "Scope1-green", "Schedule1-green" ]; 

I want to be able to store the values ​​after - in a variable, as shown below. Any ideas on how to do this using javascript or jQuery?

 var Budget1 = 'green'; var Team1 = 'green'; var Risk1 = 'green'; var Benefit1 = 'green'; var Scope1 = 'green'; var Schedule1 = 'green'; 
+6
source share
8 answers

try it

This loop goes through each element of the array, splits it and saves it as an object. You can later name these values ​​as follows

 objVariables["Budget1"] // returns green 

 var values = ["Budget1-green", "Team1-green", "Risk1-green", "Benefit1-green", "Scope1-green", "Schedule1-green"]; var objVariables = {}; for(var x=0;x<values.length;x++){ var splitted = values[x].split("-"); objVariables[splitted[0]] = splitted[1]; } console.log(objVariables); // Calling each variables // They all will return green since it is the data you have given console.log(objVariables["Budget1"]); console.log(objVariables["Team1"]); console.log(objVariables["Risk1"]); console.log(objVariables["Benefit1"]); console.log(objVariables["Scope1"]); 
+4
source

try this (save in one variable, which is a map with values ​​up to - as a key)

 var values = ["Budget1-green", "Team1-green", "Risk1-green", "Benefit1-green", "Scope1-green", "Schedule1-green"]; var map = {}; values.forEach( function(val){ var split = val.split("-"); map[ split[0] ] = split[1]; }); console.log(map); 
+2
source

You can save values ​​in an object.

Array.forEach

 var values = ["Budget1-green", "Team1-green", "Risk1-green", "Benefit1-green", "Scope1-green", "Schedule1-green"] var result = {} values.forEach(function(item){ var o = item.split("-"); result[o[0]] = o[1]; }); document.write("<pre>" +JSON.stringify(result,0,4)+ "</pre>") 

for ... of

 var values = ["Budget1-green", "Team1-green", "Risk1-green", "Benefit1-green", "Scope1-green", "Schedule1-green"] var result = {} for(var item of values){ var o = item.split("-"); result[o[0]]=o[1]; } document.write("<pre>" +JSON.stringify(result,0,4)+ "</pre>") 
+2
source

Using a non-object approach, to get exactly the solution you were looking for would be as follows (using eval() or a window object):

 var values = [ "Budget1-green", "Team1-green", "Risk1-green", "Benefit1-green", "Scope1-green", "Schedule1-green" ]; // Loop through each array item for (var i=0; i<values.length; i++) { // This splits the data into separate pieces var split_data = values[i].split('-'); // Here, we assign the dynamic variable using the window object // This is the preferred method: window[split_data[0]] = split_data[1]; // Alternatively, you can use `eval` to create the variable // This approach is generally unsafe, so use with caution: eval("var " + split_data[0] + "=" + split_data[1] + ";"); } 

Using a window object is the preferred method, as eval can potentially be dangerous.

Also, be careful when using dynamically created variables, as this can cause confusion if, for example, a variable named "Budget1" has already been specified in the global area.

+2
source

All answers so far should add to the object, and this works, but if you need / need a global scope, you can do this:

 var values = ["Budget1-green", "Team1-blue", "Risk1-green", "Benefit1-green", "Scope1-green", "Schedule1-green"]; for(var i = 0, numValues = values.length; i < numValues; ++i) { var parts = values[i].split("-"); window[parts[0]] = parts[1]; } console.log(Budget1); console.log(Team1); console.log(Risk1); console.log(Benefit1); console.log(Scope1); console.log(Schedule1); 
+1
source
 var values = ["Budget1-green", "Team1-green", "Risk1-green", "Benefit1-green", "Scope1-green", "Schedule1-green"] var obj = {}; values.forEach(function(val){ obj[val.split("-")[0]] = val.split("-")[1]; }); 

The following is the script: https://jsfiddle.net/hzdwfkue/1/

0
source

It is possible to create a new variable name on the fly (using eval() ), but is highly recommended. Instead, storing data in an object will be much better.

By going through the array, you can do it like this:

 var myVars = {}; values.forEach(function(value) { var split = value.split('-'); myVars[split[0]] = split[1]; }); 

Now you have an object that looks like this:

 { Budget1: "green", Team1: "green", etc... } 
0
source

You can do split and eval .

The eval () function evaluates or executes an argument or javascript instructions.

 var values = [ "Budget1-green", "Team1-green", "Risk1-green", "Benefit1-green", "Scope1-green", "Schedule1-green" ]; for(var i in values){ temp = values[i].split("-"); eval("var "+temp[0]+"='"+temp[1]+"';"); } console.log(Budget1);//green console.log(Scope1);//green 
0
source

All Articles