Using a non-object approach, in order to get exactly the solution you were looking for, would be as follows (using eval()
or the 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 the window object is the preferred method since eval
can potentially be dangerous.
Also, be careful when using dynamically-created variables because it can cause confusion if, for example, there was already a variable named "Budget1" in the global scope.