拆分一组字符串并存储在动态变量中

时间:2016-06-01 14:08:11

标签: javascript jquery

我从字符串拆分中得到一组如下所示的值

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

我希望能够将-之后的值存储在如下变量中。关于如何使用javascript或jQuery执行此操作的任何想法?

var Budget1   = 'green';
var Team1     = 'green';
var Risk1     = 'green';
var Benefit1  = 'green';
var Scope1    = 'green';
var Schedule1 = 'green';

8 个答案:

答案 0 :(得分:4)

试试这个

循环遍历数组中的每个元素,将其拆分,并将其存储为对象。稍后您可以像这样调用这些值

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"]);
&#13;
&#13;
&#13;

答案 1 :(得分:2)

尝试这个(存储在一个变量中,这个变量是一个前面带有值的映射 - 作为键)

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 :(得分:2)

您可以在对象中保存值。

Array.forEach

&#13;
&#13;
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>")
&#13;
&#13;
&#13;

用于...的

&#13;
&#13;
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>")
&#13;
&#13;
&#13;

答案 3 :(得分:2)

使用非对象方法,为了获得您正在寻找的解决方案,将如下(使用eval()或窗口对象):

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] + ";");
}

使用window对象是首选方法,因为eval可能有危险。

另外,使用动态创建的变量时要小心,因为例如,如果全局范围内已经存在名为“Budget1”的变量,则会引起混淆。

答案 4 :(得分:1)

到目前为止,所有答案都是添加到一个对象中,但是如果你需要/想要全局范围,你可以这样做:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 5 :(得分:0)

Any

以下是小提琴: https://jsfiddle.net/hzdwfkue/1/

答案 6 :(得分:0)

可以动态创建新的变量名称(使用eval())但强烈建议不要使用。相反,将数据存储在对象中会好得多。

通过循环遍历数组,您可以这样做:

var myVars = {};

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

现在你有一个看起来像这样的对象:

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

答案 7 :(得分:0)

您可以spliteval完成。

  

eval()函数评估或执行参数或javascript语句。

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