-2

I have a comma separated string in JavaScript that I want to separate into mutiple arrays, for each column but I want to ignore the first couple of lines. So for instance I want to convert the following string,

let data = "test,data,\n,ignore,this,\n,A,B,C,\n,1,2,3,\n,1,2,3";

into arrays like the following.

["A", "1", "1"]
["B", "2", "2"]
["C", "3", "3"]

EDIT

Ths is my initial solution that I tried. Like it works but it's not really a nice solution:/

for (let i = 1; i < out.length; i++)
{
   let arr = out[i].split(',');
   if (i === 1)
   {
       for (let j = 0; j < columns; j++)
       {
         let col = "arr" + j;
         console.log(col);
         obj[col] = [arr[j]];
       }
       console.log(obj);
   }
   else
   {
      for (let j = 0; j < columns; j++)
      {
        let col = "arr" + j;
        let val = arr[j];
        if (j !== "")
        {
            obj[col].push(val);
        }
      }
   }
}

I should point out that I eventually want to create a map of the letters to corresponding array of numbers and I won't know what the key value will be. So I'll be trying to get something like the following,

"A": ["1", "1"]
"B": ["2", "2"]
"C": ["3", "3"]
sean le roy
  • 571
  • 1
  • 7
  • 19

3 Answers3

2

You could split by ',\n,' for getting lines and for the items split by comma. Then omit the first two arrays.

var data = "test,data,\n,ignore,this,\n,A,B,C,\n,1,2,3,\n,1,2,3",
    result = data.split(',\n,').map(s => s.split(',')).slice(2);
    
console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • also here, with warning about data being valid (no stray quotes) https://stackoverflow.com/a/33156348/1692270 – JohnC May 25 '18 at 10:06
  • Thanks Nina,That's a pretty good solution. It cleaner than the one I have above. It seems a regular expression might be best from what I'm reading when searching for a solution, so will probably give that a go. – sean le roy May 25 '18 at 11:35
1

for your expected result you first have to split a string by ',' and then run for loop on a resulted array and inside that convert, you alphabet with a number and compare numbers if match found than push it into a respective array. like below code

var datArray= [];
a = [];
b = [];
c = [];
let data = "test,data,\n,ignore,this,\n,A,B,C,\n,1,2,3,\n,1,2,3";
datArray = data.split(',');
for(var i = 0; i < datArray.length; i++) {
  if(datArray[i] == 'A' || datArray[i] == 1) {
    a.push(datArray[i]);
  } else if(datArray[i] == 'B' || datArray[i] == 2) {
    b.push(datArray[i]);
  } else if(datArray[i] == 'C' || datArray[i] == 3) {
    c.push(datArray[i]);
  }
}
console.log(a);
console.log(b);
console.log(c);

this is one of the way you can do...

Hardik
  • 33
  • 7
0

This method is not hard coded ! With this method you can handle : ABCDEF.... , 1 2 3 4 5 6 ...

We will split for first action. Then detect Not A Number function isNaN to detect A B C .

Array helpers : var notNumber = []; var numbers = [];

to store data .

On the end generate your results arrays !

Try this :

var data = "test,data,\n,ignore,this,\n,A,B,C,\n,1,2,3,\n,1,2,3";
var handler = data.split(",");
var preventFlag = true;
var countNaN = 0;
var notNumber = [];
var numbers = [];

//console.log(handler);
 
for (var x = 0;x < handler.length;x++) {

  var currentData = handler[x];
    
  if (preventFlag == false) {
  
    if ( isNaN(currentData) ) {
        notNumber.push(currentData);
    }
    else {
       if (currentData != "\n") {   
         numbers.push(currentData);
       }
    }
    
  }

  if (currentData == "this"){
     preventFlag = false;
  }


}

//console.log(notNumber)
//console.log(numbers)

for (var z = 0; z < notNumber.length;z++) {

 
    window["result" + z] = [];
    window["result" + z].push(notNumber[z]);
        
    //console.log(window["result0"])
        
   window["result" + z].push(numbers[z])
   window["result" + z].push(numbers[z + (notNumber.length) ])

}

// GENERATE RESULT ARRAY

  console.log(window["result0"]);
  console.log(window["result1"]);
  console.log(window["result2"]);

//["A", "1", "1"]
//["B", "2", "2"]
//["C", "3", "3"]
Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75