-6

I have a string of ID's wherein they're being separated with commas. For example, the string is: "15,14,12,13"

How can I extract the numbers/id's from this string and save each of them in a JSON or array to be something like this

Array: {
   15,
   14,
   12,
   13
}

I don't know how it's done using regex or string manipulation. Please advice.

Jay Marz
  • 1,861
  • 10
  • 34
  • 54

1 Answers1

1

use split & map & parseInt methods.

var numbers="15,14,12,13";
var result=numbers.split(',').map(function(number){
  return parseInt(number);
});
console.log('convert '+JSON.stringify(numbers)+" to array:"+JSON.stringify(result));

Use eval method

var numbers="15,14,12,13";
var result=eval("["+numbers+"]");
console.log('convert '+JSON.stringify(numbers)+" to array:"+JSON.stringify(result));
holi-java
  • 29,655
  • 7
  • 72
  • 83