How to split a comma separated string and process in a loop using JavaScript
My two cents, adding trim to remove the initial whitespaces left in sAc's answer.
var str = 'Hello, World, etc';
var str_array = str.split(',');
for(var i = 0; i < str_array.length; i++) {
// Trim the excess whitespace.
str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, "");
// Add additional code here, such as:
alert(str_array[i]);
}
Edit:
After getting several upvotes on this answer, I wanted to revisit this. If you want to split on comma, and perform a trim operation, you can do it in one method call without any explicit loops due to the fact that split
will also take a regular expression as an argument:
'Hello, cruel , world!'.split(/\s*,\s*/);
//-> ["Hello", "cruel", "world!"]
This solution, however, will not trim the beginning of the first item and the end of the last item which is typically not an issue.
And so to answer the question in regards to process in a loop, if your target browsers support ES5 array extras such as the map
or forEach
methods, then you could just simply do the following:
myStringWithCommas.split(/\s*,\s*/).forEach(function(myString) {
console.log(myString);
});
split and flatten array of comma separated strings
In this case you could just join and split again:
const a = ["jan,feb,may", "apr,may,sept,oct", "nov,jan,mar", "dec", "oct,feb,jan"];
const res = a.join().split(',');
console.log(res);
How can I convert a comma-separated string to an array?
var array = string.split(',');
MDN reference, mostly helpful for the possibly unexpected behavior of the limit
parameter. (Hint: "a,b,c".split(",", 2)
comes out to ["a", "b"]
, not ["a", "b,c"]
.)
Split string by commas only not leaving whitespaces
You could split by comma and possible whitespace directly.
const split = s => s.split(/,\s*/);
console.log(split('20px, 40px'));
console.log(split('20px 40px'));
How can I parse a comma separated string into two variables using JavaScript?
var a = '2008, 1993';
var array = a.split(',');
var b = parseInt(array[0]);
var c = parseInt(array[1]);
http://jsfiddle.net/isherwood/X57KE/
How to split a string with comma separated pairs of colon separated keys and values and pass it into a javascript object?
An alternative solution improves the use of regex you made. This could be done by capturing the groups delimited by :
and ,
maintaining the order of an object. Look:
const string = "a_key : a_value, b_key: b_value, c_key: c_value";
const obj = {}, re = new RegExp('(.*?):(.*?)(?:,|$)','g')
string.replace(re, (_, key, value) => obj[key.trim()] = value.trim())
console.log(obj)
How to convert a string of comma separated numbers to integers?
First split the values with comma using the split()
method like this.
arr.split(',')
After that you will get each value separated in an array which will be accessible by giving array index. So arr[0]
will include 108 and arr[1]
will include 109. Now all that's left to do is parse them individually.
parseInt(arr[0])
parseInt(arr[1])
Related Topics
How to Detect Faces Using Ruby
How to Auto Hide Alert Box After It Showing It
Passing Variable from JavaScript to Ruby on Rails
How to Get All Arguments of a Function as Single Object Inside That Function
IE8 Var W= Window.Open() - "Message: Invalid Argument."
Converting JSON Format to CSV to Upload Data Table in R to Produce D3 Bubble Chart
How to Linebreak an Svg Text Within JavaScript
Using Enter Key with Action Button in R Shiny
Passing Array of Objects from Js to Rails
Sort JavaScript Array by Two Numeric Fields
Shiny App:Disable Downloadbutton
How to Prevent the Backspace Key from Navigating Back
How to Split a Long Regular Expression into Multiple Lines in JavaScript