Javascript:将字符串拆分为2d数组

时间:2022-08-27 07:30:38

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?

有没有办法压缩该代码,以便我永远不需要使用monthArray var?

4 个解决方案

#1


9  

You can use replace to get more compact code:

您可以使用replace来获得更紧凑的代码:

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);
})

#2


4  

If condensed is what you're after:

如果压缩是你所追求的:

var month_array = months.split(",").map(function(x){return x.split("_")});

#3


3  

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.

JavaScript是另一种动态语言,它的变量类型允许您随意保存任何内容。你做了正确的拆分,现在只需用_拆分该字符串并将其放回原处。

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!

我不知道你不使用monthArray是什么意思。上面的代码是你可以做的最少的,可能!

#4


1  

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]]
*/

#1


9  

You can use replace to get more compact code:

您可以使用replace来获得更紧凑的代码:

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);
})

#2


4  

If condensed is what you're after:

如果压缩是你所追求的:

var month_array = months.split(",").map(function(x){return x.split("_")});

#3


3  

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.

JavaScript是另一种动态语言,它的变量类型允许您随意保存任何内容。你做了正确的拆分,现在只需用_拆分该字符串并将其放回原处。

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!

我不知道你不使用monthArray是什么意思。上面的代码是你可以做的最少的,可能!

#4


1  

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]]
*/