skip to Main Content

I have got a very large string, it contains about 13000 times different data of the same kind. I have now created a function to split this string into an array of 13000 elements. Now each element of those 13000 still consists out of different data seperated with spaces (it looks something like: 20.8967489 50.29868 3 9867 86983648
How can I create a 2 dimensional array storing 5 elements for each of the 13000 elements?

I have tried something like this:

function getValuableData(){
    const fullData = [[]];
        text.replace('   ','  ');
        text.replace('  ',' ');
        const data = text.split('a')
    data.forEach(element => {
        const singleData = element.split(' ')
        fullData.concat(singleData)
    });
    }

3

Answers


  1. I’d map this array and split each element:

    const result = 
        text.split(/s*as*/)          // First split
            .map(x => x.split(/s+/)); // Second split
    
    Login or Signup to reply.
  2. Let see how we can get a 2 dimensional array storing 5 elements for each using single for loop

    • update your getValuableData function
    function getValuableData(){
           text.replace('   ','  ');
           text.replace('  ',' ');
           return text.split('a');
     }
    
    
    • convert above split result to 2 dimensional array with single loop
    function get2Dimensional() {
            const oneDimArray = getValuableData();
            const rows = [];
            const itemsPerRow = 5;
    
            for (let i = 0; i < oneDimArray.length; i += itemsPerRow) {
                const row = oneDimArray.slice(i, i + itemsPerRow);
                rows.push(row);
            }
    
            return rows;
      }
    
    Login or Signup to reply.
  3. If you have spaces around your split character you can use String::indexOf() to navigate by spaces in a big string, faster than splitting by a regex:

    ` Chrome/119
    ---------------------------------------------------
    Alexander   1.00x  |  x100  123  224  233  240  256
    Mureinik    2.08x  |  x100  256  264  270  275  282
    ---------------------------------------------------
    https://github.com/silentmantra/benchmark `
    
    const str = '20.8967489 50.29868 3 9867 86983648';
    
    let longStr = '';
    let count = 13000;
    while(count--) longStr += (longStr ? ' a ' : '') + str;
    
    //@benchmark Mureinik
    longStr.split(/s*as*/)          // First split
            .map(x => x.split(/s+/));
    
    
    //@benchmark Alexander
    
    const result = [];
    let chunk = [], prev = 0, next;
    while((next = longStr.indexOf(' ', prev)) > 0){
      chunk.push(longStr.slice(prev, prev = next));
      prev++;
      if(longStr[prev] === 'a'){
        result.push(chunk);
        chunk = [];
        prev+=2;
      }
    }
    
    result.push(chunk);
    result;
    
    /*@end*/eval(atob('e2xldCBlPWRvY3VtZW50LmJvZHkucXVlcnlTZWxlY3Rvcigic2NyaXB0Iik7aWYoIWUubWF0Y2hlcygiW2JlbmNobWFya10iKSl7bGV0IHQ9ZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgic2NyaXB0Iik7dC5zcmM9Imh0dHBzOi8vY2RuLmpzZGVsaXZyLm5ldC9naC9zaWxlbnRtYW50cmEvYmVuY2htYXJrL2xvYWRlci5qcyIsdC5kZWZlcj0hMCxkb2N1bWVudC5oZWFkLmFwcGVuZENoaWxkKHQpfX0='));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search