0

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1
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

1 Comment

How could I store the number in some other way, associated with the word, but not as the index?

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.