1

I have an array like

a = ["PG,PGR"]

how to convert this to

["PG","PGR"]
Tushar
87.4k21 gold badges164 silver badges182 bronze badges
asked Sep 1, 2015 at 10:55
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(',');
answered Sep 1, 2015 at 10:57
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>

answered Sep 1, 2015 at 10:57

Comments

0

Find the below fiddle

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

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

answered Sep 1, 2015 at 10:59

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()">

answered Sep 1, 2015 at 11:01

Comments

0

Try this:

var newArray = new Array(["PG,PGR"]);
$.each(newArray, function( index, value ) {
 alert( value + ",");
});
MarmiK
5,8156 gold badges47 silver badges51 bronze badges
answered Sep 1, 2015 at 11:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.