Search code examples
javascript

How to split string by rows and column in Javascript


I have an below string with two delimeter."|" is used for rows and "*" is used for columns.I would like to know how to split these string by rows and column in JS Code

"1*264.75|2*4936.00|3*8230.76|4*8329.75|5*3106.25|6*3442.00|7*5122.50|10*77.00|11*7581.00|12*7573.25|13*3509.00|21*5246.50|24*4181.00|25*4961.25|52*34.00|"

Solution

  • You could use the split function twice to create nested array:

    let str = "1*264.75|2*4936.00|3*8230.76|4*8329.75|5*3106.25|6*3442.00|7*5122.50|10*77.00|11*7581.00|12*7573.25|13*3509.00|21*5246.50|24*4181.00|25*4961.25|52*34.00|";
    
    
    let arr = str.split("|").map(r => r.split("*"));
    
    console.log(arr);