Javascript/Jquery Convert string to array

后端 未结 4 1533
北恋
北恋 2021-02-04 03:07

i have a string

var traingIds = \"${triningIdArray}\";  // ${triningIdArray} this value getting from server 
alert(traingIds)  // alerts [1,2]
var type = typeof         


        
4条回答
  •  一生所求
    2021-02-04 03:56

    check this out :)

    var traingIds = "[1,2]";  // ${triningIdArray} this value getting from server 
    alert(traingIds);  // alerts [1,2]
    var type = typeof(traingIds);
    alert(type);   // // alerts String
    
    //remove square brackets
    traingIds = traingIds.replace('[','');
    traingIds = traingIds.replace(']','');
    alert(traingIds);  // alerts 1,2        
    var trainindIdArray = traingIds.split(',');
    
    ​for(i = 0; i< trainindIdArray.length; i++){
        alert(trainindIdArray[i]); //outputs individual numbers in array
        }​ 
    

提交回复
热议问题