Convert string with commas to array



  • 3 Answer(s)

    Using  JSON.parse.

    var array = JSON.parse("[" + string + "]");
    

    Here  display Array of numbers.

    [0, 1]
    

    Using  .split() , it will stop with  Array of strings.

    ["0", "1"]
    

    Use  JSON.parse to  check  the supported data types. Here  needs  values like undefined or functions, must use eval(), or a JavaScript parser.

    Using  .split(),  also need an Array of Numbers, you could use Array.prototype.map, yet block and  write a traditional loop.

    var array = string.split(",").map(Number);
    
    Answered on December 19, 2018.
    Add Comment

    Split this  on the , character;

    var string = "0,1";
    var array = string.split(",");
    alert(array[0]);
    
    Answered on December 19, 2018.
    Add Comment

    The solution is simply accomplished in ES6;

    Here  disciple strings to Arrays with Array.from(‘string’);

    Array.from("01")
    

    desire  console.log

    ['0', '1']
    
    Answered on December 19, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.