var temp_string = "One, Two, Three, Four, Five"
var temp_arr = new Array();
temp_arr = res.CustomerAssignGroupIds.split(", ");
console.log(temp_arr)
<!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
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));