简体   繁体   中英

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:

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:

 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

 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.

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.

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.

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:

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' ] ]

This assumes that the data is 4 element aligned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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