Use the Array map method to return a modified version of your array:
var newArray = theArray.map(function(v,i,a){
return v[0].split(",");
});
The function that is passed as the argument to the map method is used to determine the values in the mapped array. As you can see, the function takes each value in the array, splits it by comma, and returns the resulting array of two strings.
The output is then:
[["Split", "each"],["string", "in"],["this", "array"]];
To make this work recursively for arrays of arbitrary depth, you can use:
var newArray = theArray.map(function mapper(v,i,a){
if(typeof v == "string"){
return v.split(",");
} else {
return v.map(mapper);
}
});
Answer from user1726343 on Stack Overflowjavascript - Split an array of strings using a separator - Stack Overflow
Split string into individual characters?
How to Split an Array of Strings ๐ป
How to split a string and place each splitted part in array?
Videos
Use the Array map method to return a modified version of your array:
var newArray = theArray.map(function(v,i,a){
return v[0].split(",");
});
The function that is passed as the argument to the map method is used to determine the values in the mapped array. As you can see, the function takes each value in the array, splits it by comma, and returns the resulting array of two strings.
The output is then:
[["Split", "each"],["string", "in"],["this", "array"]];
To make this work recursively for arrays of arbitrary depth, you can use:
var newArray = theArray.map(function mapper(v,i,a){
if(typeof v == "string"){
return v.split(",");
} else {
return v.map(mapper);
}
});
You can do this using a traditional for loop:
var theArray = [["Split,each"],["string, in"],["this","array"]];
for(var i = 0; i<theArray.length; i++) {
theArray[i] = theArray[i].split(",");
}
I'd steer clear of using the map method, it doesn't have great support. (IE < 9 doesn't support it)