Javascript - 具有Split功能的数组

时间:2012-08-28 18:14:54

标签: javascript arrays split

我无法使用Javascript和Split将信息放入数组中。

var LigneTab= new Array(3,7); //4 Lines, 7 Items
var reg=new RegExp(" +", "g");

Ligne = ("55062 5453457.4676 236746.6682 472.4027 POA 2012-08-14 GM33P086"); //First Line
LigneTab[0]=Ligne.split(reg); //Split the line in 7 items and place it in line 0
UltraEdit.messageBox(LigneTab[0,4]]); // Debug msgbox from UltraEdit to show the item 4 'POA'

3 个答案:

答案 0 :(得分:1)

在javascript中,这不一定非常复杂:

var Ligne = "55062 5453457.4676 236746.6682 472.4027 POA 2012-08-14 GM33P086"
   ,LigneTab = [Ligne.split(/\s+/)];
   // now LigneTab[0] is:
   // ["55062", "5453457.4676", "236746.6682", "472.4027", ..., "GM33P086"]

甚至:

var Ligne = "55062 5453457.4676 236746.6682 472.4027 POA 2012-08-14 GM33P086"
             .split(/\s+/);
// Ligne[0]:
// ["55062", "5453457.4676", "236746.6682", "472.4027", ..., "GM33P086"]

答案 1 :(得分:0)

首先,将数组初始化为[3, 7],然后将第0个值替换为实际需要的数组,嵌套:

LigneTab[0]=Ligne.split(reg); //Split the line in 7 items and place it in line 0

所以LigneTab实际上是[["55062","5453457.4676","236746.6682","472.4027","POA","2012-08-14","GM33P086"], 7],索引4没有值。

其次,如果确实有>因为表达式

,所以4个元素LigneTab[0,4]没有多大意义
0, 4

评估为4,因此您不妨只写LigneTab[4]

你可能想要这个:

var LigneTab = Ligne.split(/\s+/);
UltraEdit.messageBox(LigneTab[4]]); // Debug msgbox from UltraEdit to show the item 4 'POA'

或许,您可能希望将其作为嵌套列表,在这种情况下您需要:

var LigneTab[0] = Ligne.split(/\s+/);
UltraEdit.messageBox(LigneTab[0][4]);

答案 2 :(得分:0)

考虑到你发布的代码,我不明白为什么你需要一个二维数组。但是如果你真的需要一个,那么你在这里尝试是创建它并访问它的可能方法之一:

var LigneTab = []; // one-dimensional for now
var reg=new RegExp(" +", "g");
var Ligne = "55062 5453457.4676 236746.6682 472.4027 POA 2012-08-14 GM33P086"; 
LigneTab[0] = Ligne.split(reg); 
// Now LigneTab is two-dimensional. 
// LigneTab[0] contains another array with 7 items
UltraEdit.messageBox(LigneTab[0][4]]);