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';
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.
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.
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.
Yes, . split() always preserves the order of the characters in the string.
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"]);
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);
You can save values in object.
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>")
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>")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With