Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a set of strings and storing in a dynamic variable

I am getting a set of values like below from doing a split of strings

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

I want to be able to store the values after the - in a variable like below. Any ideas on how to do this with javascript or jQuery?

var Budget1   = 'green';
var Team1     = 'green';
var Risk1     = 'green';
var Benefit1  = 'green';
var Scope1    = 'green';
var Schedule1 = 'green';
like image 656
Baba Avatar asked Jun 01 '16 14:06

Baba


People also ask

How do you split and store strings?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do you split a string into values?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

What does splitting a string mean?

String splitting is the process of breaking up a text string in a systematic way so that the individual parts of the text can be processed. For example, a timestamp could be broken up into hours, minutes, and seconds so that those values can be used in the numeric analysis.

Does string split preserve order?

Yes, . split() always preserves the order of the characters in the string.


3 Answers

Try this

This loops through each element in the array , splits it , and stores it as an object. Later you can call these values like this

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"]);
like image 199
Akshay Avatar answered Nov 04 '22 11:11

Akshay


try this (store in one variable which is a map with values before - as 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);
like image 43
gurvinder372 Avatar answered Nov 04 '22 10:11

gurvinder372


You can save values in 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>")
like image 32
Rajesh Avatar answered Nov 04 '22 09:11

Rajesh