简体   繁体   English

将字符串转换为数组数组

[英]Converting a string into an array of arrays

I have a string of data which look like this 我有一串看起来像这样的数据

0E-11 GERBER CA 960350E-110.0215.500000000 0E-12 TEHAMA CA 960900E-11214.800000000

I want to convert this string into an array of arrays. 我想将此字符串转换为数组数组。

Please note that after every 4 elements this array should be divided into new array and the end result should look like this: 请注意,每4个元素后,此数组应划分为新数组 ,最终结果应如下所示:

Desire Results: 愿望结果:

this.tblData: Array(2)
            0: ["0E-11,"GERBER", "CA", "960350E-110.0215.500000000"]
            1:["0E-12", "TEHAMA", "CA" ,"960900E-11214.800000000"]

Thanks 谢谢

You can use the remainder operator and a forEach loop on that string to build an array of arrays, where each nested array is created every n steps: 您可以在该字符串上使用剩余运算符和一个forEach循环来构建一个数组数组,其中每n个步骤都会创建一个嵌套数组:

 var result = []; "0E-11 GERBER CA 960350E-110.0215.500000000 0E-12 TEHAMA CA 960900E-11214.800000000".split(" ").forEach(function(element, index, array) { if( (index + 1) % 4 === 0) { result.push([ array[index-3], array[index-2], array[index-1], array[index] ]); } }); console.log(result); 

You can use reduce for such purposes 您可以将reduce用于此类目的

 let result = "0E-11 GERBER CA 960350E-110.0215.500000000 0E-12 TEHAMA CA 960900E-11214.800000000" .split(" ") // split the string based on the spaces .reduce((current, item) => { if (current[current.length - 1].length === 4) { // in case the result array has already 4 items in it // push in a new empty array current.push([]); } // add the item to the last array current[current.length - 1].push(item); // return the array, so it can either be returned or used for the next iteration return current; }, [ [] ]); // start value for current would be an array containing 1 array console.log(result); 

It starts by splitting your string by spaces, creating an array of the values, and then we can transform the result using the reduce function. 首先用空格分隔字符串,创建值数组,然后我们可以使用reduce函数转换结果。

The reduce function will take the second parameter as a start value for the current argument, which will start as an array containing 1 empty array. reduce函数将第二个参数作为当前参数的起始值,该参数将从一个包含1个空数组的数组开始。

Inside the reducer it first check if the last item in the array has a length of 4, in case it does, add the next sub array to the array, and will then push the current item inside the last array. 在reducer内部,它首先检查数组中的最后一个项目的长度是否为4(如果确实如此),则将下一个子数组添加到数组中,然后将当前项目推入最后一个数组中。

The result will then be an array containing your arrays 结果将是包含您的数组的数组

There is no need to use modulus operator, simply increment the loop's counter by 4: 无需使用模运算符,只需将循环计数器增加4:

var original = [
    '0E-11',
    'GERBER',
    'CA',
    '960350E-110.0215.500000000',
    '0E-12',
    'TEHAMA',
    'CA',
    '960900E-11214.800000000'
];

var result = [];

for (var i = 0; i < original.length; i += 4) {
    result.push([
        original[i],
        original[i+1],
        original[i+2],
        original[i+3],
    ]);
}

console.log(result);

Output: [ [ '0E-11', 'GERBER', 'CA', '960350E-110.0215.500000000' ], [ '0E-12', 'TEHAMA', 'CA', '960900E-11214.800000000' ] ] 输出: [ [ '0E-11', 'GERBER', 'CA', '960350E-110.0215.500000000' ], [ '0E-12', 'TEHAMA', 'CA', '960900E-11214.800000000' ] ]

This assumes that the data is 4 element aligned. 假设数据是4个元素对齐的。

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

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