0

I am trying to make a string which looks like luis,philip .It having comma in between them but i have to make an array with it.I am trying with json.Parse but it is not working i am posting my code

var names = 'luis,philip' ;
var nameArray = JSON.parse("[" + names +"]");

but it showing error in json parsing ,please somebody help

Subho
  • 921
  • 5
  • 25
  • 48

2 Answers2

2

Just use the split method:

var names = 'luis,philip';
var namesArray = name.split(',');
britter
  • 1,352
  • 1
  • 11
  • 26
1

Use split to split the string and get the array.

var names = 'luis,philip';
var arr = names.split(','); // ['luis', 'philip']
Tushar
  • 85,780
  • 21
  • 159
  • 179