如何将内容从2D数组拆分为另一个2D Javascript

时间:2021-11-08 04:30:35

I have a webpage that insert a table with a formated 2D array content. It looks like this (a string):

我有一个网页插入一个表格,其中包含格式化的2D数组内容。它看起来像这样(一个字符串):

type[separator1]product1 Name[separator1]product 1 price[separator1]This is product1, as description[separator2]type[separator1]product2 Name[separator1]product 2 price[separator1]This is product2, as description

类型[separator1] product1名称[separator1]产品1价格[separator1]这是product1,如描述[separator2]类型[separator1] product2名称[separator1]产品2价格[separator1]这是product2,作为描述

As you can see, I have a separator (formated), they are [separator1] and [separator2]. It is converted from normal 2D array, and when I manipulate it to make some 2D array, I got this:

如您所见,我有一个分隔符(格式化),它们是[separator1]和[separator2]。它是从普通的2D数组转换而来的,当我操作它来制作一些2D数组时,我得到了这个:

[
"type[separator1]product1 Name[separator1]product 1 price[separator1]This is product1, as description",
"type[separator1]product2 Name[separator1]product 2 price[separator1]This is product2, as description"
]

Now, you can see that [separator1] is for separating array's content. And [separator2] is for separating between arrays. And you also can see that on my blockquoted 2D array formated content, the description has coma on it, which is becoming a problem because I want to split that inner array so I can get like array2[1], array2[2], etc. but I got problems, it said split is not a function. I want to access that 2D array from that separator so I can access array3[0][1] and others from that first String

现在,您可以看到[separator1]用于分隔数组的内容。 [separator2]用于分隔数组。你也可以看到,在我的块状二维数组格式化内容中,描述有昏迷,这就成了一个问题因为我想拆分那个内部数组,所以我可以得到像array2 [1],array2 [2]等但是我遇到了问题,它说分裂不是一个功能。我想从该分隔符访问该2D数组,以便我可以从第一个String访问array3 [0] [1]和其他人

This is the sample code

这是示例代码

$('#tester').on('click', function() {
	var iContent = 'type[separator1]product1 Name[separator1]product 1 price[separator1]This is product1, as description[separator2]type[separator1]product2 Name[separator1]product 2 price[separator1]This is product2, as description';
	var str1 = iContent.split('[separator2]');
	var str2 = [];
	for (i=0; i<str1.length; i++) {
		str2.push(str1[i]);
	}
	//var str3 = str2.split('[separator1]');
	console.log('str2[0]: ' + str2[0]);
	console.log('str2[1]: ' + str2[1]);
	for (i=0; i<str2.length; i++) {
		str3.push(str2[i]);
	}
	console.log('iContent: ' + iContent);
	console.log('str1: ' + str1);
	console.log('str2: ' + str2);
	console.log('str3: ' + str3);
});

1 个解决方案

#1


3  

For getting an array without separators, you could split and map the splitted stings.

要获取不带分隔符的数组,可以拆分并映射分裂的stings。

var string = 'type[separator1]product1 Name[separator1]product 1 price[separator1]This is product1, as description[separator2]type[separator1]product2 Name[separator1]product 2 price[separator1]This is product2, as description',
    result = string.split('[separator2]').map(function (s) {
        return s.split('[separator1]');
    });
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#1


3  

For getting an array without separators, you could split and map the splitted stings.

要获取不带分隔符的数组,可以拆分并映射分裂的stings。

var string = 'type[separator1]product1 Name[separator1]product 1 price[separator1]This is product1, as description[separator2]type[separator1]product2 Name[separator1]product 2 price[separator1]This is product2, as description',
    result = string.split('[separator2]').map(function (s) {
        return s.split('[separator1]');
    });
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }