Convert string with commas to array

后端 未结 18 884
执念已碎
执念已碎 2020-11-22 12:08

How can I convert a string to a JavaScript array?

Look at the code:

var string = \"0,1\";
var array = [string];
alert(array[0]);

In

相关标签:
18条回答
  • 2020-11-22 12:11

    Split (",") can convert Strings with commas into a String array, here is my code snippet.

        var input ='Hybrid App, Phone-Gap, Apache Cordova, HTML5, JavaScript, BootStrap, JQuery, CSS3, Android Wear API'
        var output = input.split(",");
        console.log(output);
    

    ["Hybrid App", " Phone-Gap", " Apache Cordova", " HTML5", " JavaScript", " BootStrap", " JQuery", " CSS3", " Android Wear API"]

    0 讨论(0)
  • 2020-11-22 12:13

    For simple array members like that, you can use JSON.parse.

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

    This gives you an Array of numbers.

    [0, 1]
    

    If you use .split(), you'll end up with an Array of strings.

    ["0", "1"]
    

    Just be aware that JSON.parse will limit you to the supported data types. If you need values like undefined or functions, you'd need to use eval(), or a JavaScript parser.


    If you want to use .split(), but you also want an Array of Numbers, you could use Array.prototype.map, though you'd need to shim it for IE8 and lower or just write a traditional loop.

    var array = string.split(",").map(Number);
    
    0 讨论(0)
  • 2020-11-22 12:13

    Regexp

    As more powerful alternative to split, you can use match

    "0,1".match(/\d+/g)
    

    let a = "0,1".match(/\d+/g)
    
    console.log(a);

    0 讨论(0)
  • 2020-11-22 12:21

    You can use javascript Spread Syntax to convert string to an array. In the solution below, I remove the comma then convert the string to an array.

    var string = "0,1"
    var array = [...string.replace(',', '')]
    console.log(array[0])
    
    0 讨论(0)
  • 2020-11-22 12:22

    Convert all type of strings

    var array = (new Function("return [" + str+ "];")());
    
    
    
    var string = "0,1";
    
    var objectstring = '{Name:"Tshirt", CatGroupName:"Clothes", Gender:"male-female"}, {Name:"Dress", CatGroupName:"Clothes", Gender:"female"}, {Name:"Belt", CatGroupName:"Leather", Gender:"child"}';
    
    var stringArray = (new Function("return [" + string+ "];")());
    
    var objectStringArray = (new Function("return [" + objectstring+ "];")());
    

    JSFiddle https://jsfiddle.net/7ne9L4Lj/1/

    Result in console

    Some practice doesnt support object strings

    - JSON.parse("[" + string + "]"); // throw error
    
     - string.split(",") 
    // unexpected result 
       ["{Name:"Tshirt"", " CatGroupName:"Clothes"", " Gender:"male-female"}", "      {Name:"Dress"", " CatGroupName:"Clothes"", " Gender:"female"}", " {Name:"Belt"",    " CatGroupName:"Leather"", " Gender:"child"}"]
    
    0 讨论(0)
  • 2020-11-22 12:24

    How to Convert Comma Separated String into an Array in JavaScript?

    var string = 'hello, world, test, test2, rummy, words';
    var arr = string.split(', '); // split string on comma space
    console.log( arr );
     
    //Output
    ["hello", "world", "test", "test2", "rummy", "words"]
    

    For More Examples of convert string to array in javascript using the below ways:

    1. Split() – No Separator:

    2. Split() – Empty String Separator:

    3. Split() – Separator at Beginning/End:

    4. Regular Expression Separator:

    5. Capturing Parentheses:

    6. Split() with Limit Argument

      check out this link ==> https://www.tutsmake.com/javascript-convert-string-to-array-javascript/

    0 讨论(0)
提交回复
热议问题