Javascript:将字符串拆分为二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8594097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:56:21  来源:igfitidea点击:

Javascript: split string into 2d array

javascriptarraysstringmultidimensional-arraysplit

提问by dmr

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 monthArrayvar?

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

回答by georg

You can use replaceto get more compact code:

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

var months= "2010_1,2010_3,2011_4,2011_7";
var monthArray2d = []

months.replace(/(\d+)_(\d+)/g, function(
monthArray2d = months.split(",").map(function(e) {
    return e.split("_").map(Number);
})
, , ) { monthArray2d.push([parseInt(), parseInt()]); })

or mapif your target browser supports it:

映射,如果您的目标浏览器支持它:

var months= "2010/1 ... 2010/3 ... 2011/4";
months.replace(/(\d+)\/(\d+)/g, function(
var month_array = months.split(",").map(function(x){return x.split("_")});
, , ) { monthArray2d.push([parseInt(), parseInt()]); })

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,2011_7";

var monthArray = months.split(",");

for (var i = 0; i < monthArray.length; i++) {
   monthArray[i] = monthArray[i].split("_");
}

console.log(monthArray);

回答by Shawn Chin

If condensed is what you're after:

如果你想要的是 condensed:

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

回答by Abdul Munim

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:

看这个例子:

##代码##

I don't know what you mean by not to use monthArray. The code above is the least you can do, probably!

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

回答by kennebec

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:

但我没有看到您的代码有任何改进:

##代码##