2

I have following string :

var str = '15156,"ABALONE, FRIED",60.1,189,19.63,,,,';

i want to split it in the following way :-

[15156, "ABALONE, FRIED", 60.1, 189, 19.63, null, null, null]

i have try this:-

var strArray = str.split(",");
output:- 
[15156, "ABALONE", "FRIED", 60.1, 189, 19.63]

How can i get this using javascript's split function or any other way.

Manish Balodia
  • 1,863
  • 2
  • 23
  • 37
  • Googled and found this: https://stackoverflow.com/questions/11456850/split-a-string-by-commas-but-ignore-commas-within-double-quotes-using-javascript – Sid Jun 13 '18 at 09:27
  • You could use `JSON.parse('[' + str + ']')` – Roland Starke Jun 13 '18 at 09:27
  • 1
    It gives error for '15156,"ABALONE, FRIED",60.1,189,19.63,,,,' – Manish Balodia Jun 13 '18 at 09:37
  • 1
    Food.insertFoodDatabase = function(data, cb) { fs.readFile('client/foodDB.csv',{encoding:'utf8'},function(err, data){ if(err) return cb(err); data = data.split('\r\n'); _.map(data,function(v,key){ v= v.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/); console.log(v); console.log("-----------------------------------------"); }) }); }; – Mukesh Kumar Bijarniya Jun 13 '18 at 10:14

1 Answers1

2

Convert that into array like string representation and you can parse it to get the desired output:

var str = '15156,"ABALONE, FRIED",60.1,189,19.63';
var res = JSON.parse('[' + str + ']')
console.log(res);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62