Retrieving list of names from list of name value pairs
I have list of paired values, and i need to get the list of just the first item:
Example:
Input: CA: San Francisco, TX:Austin, CA: San Jose, GA: Atlanta, MA: Boston
Output: CA, TX, CA, GA, MA
Can someone help me with a Javascript snippet that achieves this? I am thinking on lines of using split and join, but I am getting confused, especially because the number of attributes in the original list is not fixed.
<script>
var Input='CA: San Francisco, TX:Austin, CA: San Jose, GA: Atlanta, MA: Boston';
var arr=Input.replace(/\s+/g, '').split(',');
var ext=new Array();
for (i=0;i<arr.length;i++){
ext[i]=arr[i].substring(0,2);
}
alert(ext.join(','));
</script>
Bookmarks