Share and Learn To Write Code!

Programming articles, which help you to build your application. If something you are stuck to code and complete the program, just find here your question-related articles.

Share with   

How to convert string which is having comma separated list into array list in javascript

Here we are converting comma separated string into array list using JavaScript. So we can use this java script in every framework like php, angular, react, vue js etc.
Java Script   JS   JavaScript Array   JavaScript String   HTML  

Converting comma separated string into array list using JavaScript having 2 methods i.e.


1) Using split() method

2) Iterating with string and slicing string to comma and push to array.


Examples


Method 1 : Using split() method


Syntax for converting array

var temp_string = "One, Two, Three, Four, Five"
var temp_arr = new Array();
temp_arr = res.CustomerAssignGroupIds.split(", ");
console.log(temp_arr)


Check Sample HTML Demo

<!DOCTYPE html> 
<html> 
  <head> 
  	<title> 
  	   Convert comma separated string to array using JavaScript 
    </title> 
  </head> 
  <body> 
  	<h1 style="color: blue"> ProCodeProgramming </h1> 
  	<b>Convert comma separated string 
  	to array using JavaScript</b> 
  	<p>Original string is "One, Two, Three, Four, Five"</p> 
  	<p>Separated Array is: <span class="output"></span></p> 
  	<button onclick="separateString()"> 
  	      Remove Text 
    </button> 
  	<script type="text/javascript"> 
  		function separateString() { 
  			originalString = "One, Two, Three, Four, Five"; 
  			separatedArray = originalString.split(', '); 
  			console.log(separatedArray); 
  			document.querySelector('.output').textContent = 
  			separatedArray; 
  		} 
  	</script> 
  </body> 
</html> 


Output




Method 2 : Iterating with string and slicing string to comma and push to array.


Syntax for slicing string using for loop

originalString = "Bhushan, Amit, Maya, Pratik, Shanaya"; 
separatedArray = []; 
 
// index of end of the last string  
let previousIndex = 0; 
 
for(i = 0; i < originalString.length; i++) { 
 
    // check the character for a comma 
    if (originalString[i] == ', ') { 
 
        // split the string from the last index 
        // to the comma 
        separated = originalString.slice(previousIndex, i); 
        separatedArray.push(separated); 
 
        // update the index of the last string 
        previousIndex = i + 1; 
    } 
} 
 
// push the last string into the array 
separatedArray.push(originalString.slice(previousIndex, i));


Thank you for watching article. If you want learn more about java script please check our javascript tutorials.

Hit Like

Hit like for this post

Here About Author
Author Image
Pro Code Programming

Programming articles, which help you to build your application. If something you are stuck to code and complete the program, just find here your question-related articles.

0replies
Leave a reply
49
Registerd Users
92
Total Subscribers
59
Total Articles
60902
Total Views