i want to build an object using this code
$("input").bind("keydown",function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13){
var guess = $("input").val();
guess = guess.split(" ");
var oGuess = {};
for (var x = 0; guess.length ; x++){
oGuess[x] = oGuess[x] = {"text": guess[x]};
}
$("input").val("");
}
});
this actually crashes my browsers(latest stable ff and chrome) upon hitting enter. heres the jsfiddle http://jsfiddle.net/kfqJC/1/
i need the object to be something like this
oGuess = {
"1": { "text" : string}
"2": { "text" : string}
...
}
what am I missing here?
Your loop never terminates. If
guesshas one element or more,guess.lengthwill always evaluate totrue.I assume you want
Also, what is this for?
Just write
And if you want the properties start with
1, you have to writethought I don’t see any advantage of using an object over an array in this case.