How do I turn this string: house,33;car,43;dog's,99; into this array: arr[33]="house" arr[43]="car" arr[99]="dog's" using javascript and jquery.
Once I have the array, can I store information (like a 0 or 1 flag) next to each?
asked Apr 7, 2011 at 11:45
Patrick Beardmore
1,0321 gold badge11 silver badges35 bronze badges
2 Answers 2
try this..
var initString = "house,33;car,43;dog's,99";
var array1 = initString.split(';')
var result = [];
for(var i=0,l=array1.length;i<l;i++){
var items = array1[i].split(',');
result[parseInt(items[1])] = {flag:0, value:items[0]};
}
answered Apr 7, 2011 at 11:54
Andrei
4,2173 gold badges29 silver badges32 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
var str = "house,33;car,43;dog's,99;";
var pieces = str.split(';');
var arr = new Array();
for (var index = 0; index < pieces.length; index++) {
var halves = pieces[index].split(',');
arr[ halves[1] ] = halves[0];
}
Though you really shouldn't be specifying the array index unless you have a complete list from 0 to 99 (your sample has only three entries, all of which are well into that sequence).
answered Apr 7, 2011 at 11:53
Mike Thomsen
37.7k11 gold badges64 silver badges86 bronze badges
1 Comment
Patrick Beardmore
How could I store the number in some other way, associated with the word, but not as the index?