将2D数组拆分为3D数组JavaScript

时间:2022-08-27 07:47:45

I have some data and I want to split it twice. The data looks something like this:

我有一些数据,我想将它拆分两次。数据看起来像这样:

var data = ['a1 b1 c1', 'a2 b2 c2Xa3 b3 c3Xa4 b4 c4', 'a5 b5 c5', 'a6 b6 c6Xa7 b7 c7', 'a8 b8 c8'];

As a first delimiter I use the 'X' character and this works fine:

作为第一个分隔符,我使用'X'字符,这很好用:

var firstSplit = new Array(new Array());
    for(var i = 0; i < data.length; i++)
        firstSplit[i] = data[i].split('X');

But when I use the whitespace between the elements as a delimiter I get "Uncaught TypeError: Cannot set property '0' of undefined", on the line of the second split.

但是当我使用元素之间的空格作为分隔符时,我得到“Uncaught TypeError:无法设置未定义的属性'0',在第二个分割的行上。

var secondSplit = new Array(new Array(new Array()));
for(var i = 0; i < firstSplit.length; i++) {
    for(var j = 0; j < firstSplit[i].length; j++)
        secondSplit[i][j] = firstSplit[i][j].split(' ');
    }

4 个解决方案

#1


2  

You could first split by X and map the result of the splitted values by space .

您可以先按X拆分,然后按空格映射拆分值的结果。

var data = ['a1 b1 c1', 'a2 b2 c2Xa3 b3 c3Xa4 b4 c4', 'a5 b5 c5', 'a6 b6 c6Xa7 b7 c7', 'a8 b8 c8'],
    result = data.map(a => a.split('X').map(b => b.split(' ')));
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#2


1  

Array.map would be a better way

Array.map是一种更好的方法

var data = ['a1 b1 c1', 'a2 b2 c2Xa3 b3 c3Xa4 b4 c4', 'a5 b5 c5', 'a6 b6 c6Xa7 b7 c7', 'a8 b8 c8'];

var result = data.map( (str) => {
   return str.split(' ');
});

console.log(result);

#3


0  

Use .push to push each element to your result array.

使用.push将每个元素推送到结果数组。

var data = ['a1 b1 c1', 'a2 b2 c2Xa3 b3 c3Xa4 b4 c4', 'a5 b5 c5', 'a6 b6 c6Xa7 b7 c7', 'a8 b8 c8'];

var firstSplit = new Array(new Array());
for (var i = 0; i < data.length; i++) {
  firstSplit[i] = data[i].split('X');
}

var secondSplit = [];
for (var i = 0; i < firstSplit.length; i++) {
  for (var j = 0; j < firstSplit[i].length; j++) {
    secondSplit.push(firstSplit[i][j].split(' '));
  }
}

console.log(secondSplit)

#4


0  

Convert space to (-) then do you work

将空间转换为( - )然后你工作

var str;
var firstSplit = new Array(new Array());
for(var i = 0; i < data.length; i++){
str = data[i].replace(/\s+/g, '-');
    firstSplit[i] = str.split('-');
}

#1


2  

You could first split by X and map the result of the splitted values by space .

您可以先按X拆分,然后按空格映射拆分值的结果。

var data = ['a1 b1 c1', 'a2 b2 c2Xa3 b3 c3Xa4 b4 c4', 'a5 b5 c5', 'a6 b6 c6Xa7 b7 c7', 'a8 b8 c8'],
    result = data.map(a => a.split('X').map(b => b.split(' ')));
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#2


1  

Array.map would be a better way

Array.map是一种更好的方法

var data = ['a1 b1 c1', 'a2 b2 c2Xa3 b3 c3Xa4 b4 c4', 'a5 b5 c5', 'a6 b6 c6Xa7 b7 c7', 'a8 b8 c8'];

var result = data.map( (str) => {
   return str.split(' ');
});

console.log(result);

#3


0  

Use .push to push each element to your result array.

使用.push将每个元素推送到结果数组。

var data = ['a1 b1 c1', 'a2 b2 c2Xa3 b3 c3Xa4 b4 c4', 'a5 b5 c5', 'a6 b6 c6Xa7 b7 c7', 'a8 b8 c8'];

var firstSplit = new Array(new Array());
for (var i = 0; i < data.length; i++) {
  firstSplit[i] = data[i].split('X');
}

var secondSplit = [];
for (var i = 0; i < firstSplit.length; i++) {
  for (var j = 0; j < firstSplit[i].length; j++) {
    secondSplit.push(firstSplit[i][j].split(' '));
  }
}

console.log(secondSplit)

#4


0  

Convert space to (-) then do you work

将空间转换为( - )然后你工作

var str;
var firstSplit = new Array(new Array());
for(var i = 0; i < data.length; i++){
str = data[i].replace(/\s+/g, '-');
    firstSplit[i] = str.split('-');
}