admin管理员组文章数量:1182257
I have a string of month and years:
var months= "2010_1,2010_3,2011_4,2011_7";
I want to make this into a 2d array with the year in the first position of each array and the month in the second position. In other words, I want to end up with this:
var monthArray2d = [[2010,1],[2010,3][2011,4],[2011,7]];
The way I do this currently is:
//array of selected months
var monthArray = months.split(",");
//split each selected month into [year, month] array
var monthArray2d = new Array();
for (var i = 0; i < monthArray.length; i++) {
monthArray2d[i] = monthArray[i].split("_");
Is there a way to condense that code so that I never need to use the monthArray
var?
I have a string of month and years:
var months= "2010_1,2010_3,2011_4,2011_7";
I want to make this into a 2d array with the year in the first position of each array and the month in the second position. In other words, I want to end up with this:
var monthArray2d = [[2010,1],[2010,3][2011,4],[2011,7]];
The way I do this currently is:
//array of selected months
var monthArray = months.split(",");
//split each selected month into [year, month] array
var monthArray2d = new Array();
for (var i = 0; i < monthArray.length; i++) {
monthArray2d[i] = monthArray[i].split("_");
Is there a way to condense that code so that I never need to use the monthArray
var?
4 Answers
Reset to default 12You can use replace
to get more compact code:
var months= "2010_1,2010_3,2011_4,2011_7";
var monthArray2d = []
months.replace(/(\d+)_(\d+)/g, function($0, $1, $2) {
monthArray2d.push([parseInt($1), parseInt($2)]);
})
or map if your target browser supports it:
monthArray2d = months.split(",").map(function(e) {
return e.split("_").map(Number);
})
Basically, the first function looks for year/month patterns "digits underscore digits", and stores each found substring in an array. Of course, you can use other delimiters instead of underscore. The function doesn't care about the values' delimiter (comma), so that it can be whatever. Example:
var months= "2010/1 ... 2010/3 ... 2011/4";
months.replace(/(\d+)\/(\d+)/g, function($0, $1, $2) {
monthArray2d.push([parseInt($1), parseInt($2)]);
})
If condensed is what you're after:
var month_array = months.split(",").map(function(x){return x.split("_")});
JavaScript is another dynamic language and its variable type allows you to keep anything wherever you like. You did the split right, now just split that string with _
and put it back in there.
See this example:
var months= "2010_1,2010_3,2011_4,2011_7";
var monthArray = months.split(",");
for (var i = 0; i < monthArray.length; i++) {
monthArray[i] = monthArray[i].split("_");
}
console.log(monthArray);
I don't know what you mean by not to use monthArray
. The code above is the least you can do, probably!
Splitting each split is a sensible thing to do- you can use a regular expression to match the underscore separated numbers, and insert each pair in its own array pushed to the month array-
but I don't see an improvement over your code:
var s= "2010_1,2010_3,2011_4,2011_7",
A= [], rx=/(\d+)_(\d+)/g;
while((M= rx.exec(s))!= null){
A.push([M[1], M[2]]);
}
/* returned value: (Array)
[[2010,1],[2010,3],[2011,4],[2011,7]]
*/
本文标签: Javascript split string into 2d arrayStack Overflow
版权声明:本文标题:Javascript: split string into 2d array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738216747a2069602.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论