简体   繁体   English

拆分字符串数组后未定义(JavaScript)

[英]Undefined after splitting array of strings (JavaScript)

I wish to split an array of strings ( objData ) into individual elements and copy these to the first row of a 2D array ( sequence ). 我希望将字符串数组( objData )拆分为单个元素,然后将它们复制到2D数组的第一行( sequence )。 Please see the code below. 请参见下面的代码。

for(i = 0; i < objData.length; i++)
{
    console.log("objData[i]: " + objData[i]);
    parts = objData[i].split(' ');
    sequence[0][n] = parts[1];
    console.log("sequence[0][n]: " + sequence[0][n]);
    sequence[0][n+1] = parts[2];
    sequence[0][n+2] = parts[3];
    n+=3;
    parts = [];
}

Note n is initialised as 0. Each element of objData is a string in the form shown below: 注意n初始化为objData每个元素都是一个字符串,格式如下所示:

objData[0] = "v 2.11733 0.0204144 1.0852"

I wish to split these strings (from every element of objData ) by using the whitespace as the separator and copy the decimal values to the first row of sequence . 我希望通过使用空格作为分隔符(从objData每个元素)拆分这些字符串,并将十进制值复制到sequence的第一行。 I also want to parse these string values to floating point. 我也想将这些字符串值解析为浮点数。

Using the code above the values are being copied however the contents of the first row of sequence looks like: 使用上面的代码,将复制值,但是sequence第一行的内容如下:

[2.11733NaN, 0.0204144NaN, 1.0852NaN, 2.12303undefined, 0.0131256undefined ..... etc.]

The remaining elements all have 'undefined' appended to them as shown. 其余元素都附加了“未定义”,如图所示。

I tried using the following line to parse to floating point: 我尝试使用以下行来解析为浮点数:

parts = objData[i].split(' ').map(parseFloat);

However the contents of sequence were then: 但是, sequence的内容如下:

    [2.11733, 0.0204144, 1.0852, NaN, NaN ..... etc.]

If someone could tell me how to split and parse this data that would be much appreciated! 如果有人可以告诉我如何拆分和解析此数据,将不胜感激! Thank you. 谢谢。

It seems to me that you are making heavy work of this. 在我看来,您正在为此做大量工作。

Lets take this step by step: 让我们逐步进行此操作:

// Make sure sequence array is empty
sequence = [];

// We want to process each element of the objData array :
objData.forEach (function (obj_n) {
  // First split the element, one or more spaces separate fields
  obj_n = obj_n.split (/ +/); // obj_n is now an array
  // Extract elements 1 to 3
  obj_n = obj_n.slice (1, 4); // obj_n is now the three data values
  // Append the three elements to the sequence array:
  sequence = sequence.concat (obj_n);
});

// Now convert to numbers:
sequence = sequence.map (function (f) { return +f });

More compactly : 更紧凑:

sequence = [];
objData.forEach (function (obj_n) {
  sequence = sequence.concat (obj_n.split (/ +/).slice (1, 4));
});
sequence = sequence.map (function (f) { return +f });

Answering additional constraints in op comments below 在下面的操作注释中回答其他约束

function parseObjData (objData, m, n) { // Add n items to sequence m
  if (!sequence[m]) // make sure sequence[m] exists
    sequence[m] = [];

  objData.forEach (function (obj_n) {
    sequence[m] = sequence[m].concat (obj_n.split (/ +/).
                              slice (1, n + 1));
  });
  sequence[m] = sequence[m].map (function (f) { return +f });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM