1

I have an array like

a = ["PG,PGR"]

how to convert this to

["PG","PGR"]
3
  • Can you add your complete code of how you're getting this array? Commented Sep 1, 2015 at 10:55
  • 4
    String.prototype.split() Commented Sep 1, 2015 at 10:56
  • if that's how u r getting the string. write it the way u desire. Commented Sep 1, 2015 at 10:56

5 Answers 5

4

Use the .split(',') function. It splits a string into an array of substrings that were separated by ',' character, and return a new array.For more info about split function please visit this link.

 var a = ["PG,PGR"]
     a= a[0].split(',');
Sign up to request clarification or add additional context in comments.

Comments

0

try this : iterate array and use split(",") which will return you new array.

var a = ["PG,PGR"];
var newArray = new Array();
$.each(a , function(i,v){
   newArray.push(v.split(",")); 
});
alert(newArray);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Comments

0

Find the below fiddle

var array = ["PG,PGR"]
alert(array[0].split(','));

http://jsfiddle.net/Kishore_Indraganti/fbrkx90n/

Comments

0

The following can handle arrays like ["PG,PGR","a","asd,asdsa"] also.

function load(){
    var a = ["PG,PGR","a","asd,asdsa"];
    document.getElementById('test').value = a.join(',').split(',');
}
<input id="test" value="" type="text">
<input value="Load" type="button" onclick="load()">

Comments

0

Try this:

var newArray = new Array(["PG,PGR"]);

$.each(newArray, function( index, value ) {
  alert( value + ",");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.