I'm trying to build an array of strings, but I got an
Uncaught TypeError: Cannot set property '0' of undefined
I don't really get why, here is my code.
$("#tableLits > tbody > tr input").filter(function(){ return $(this).val() !== '' }).each(function(){
nom = $(this).attr('name');
numChambre = parseInt(nom.charAt(nom.length-1));
taille = nom.slice(nom.charAt(nom.length-2),-2).replace('lit','');
chambres[numChambre][nbLits] = taille;
nbLits = oldChambre == numChambre ? nbLits++ : 0;
oldChambre = numChambre;
});
console.log(chambres);
The error is on this line:
chambres[numChambre][nbLits] = taille;
But when I look at the debugger,
- Chambre is an array
- numChambre = 1
- nbLit = 0
My goal is to assign a size of a bed (taille) to a bed number (nbLit) in a given room (chambres)
Edit
For example, I have this: lit80n1 lit90n1 lit120n2 lit160n3
And I want to output something like this:
Chambre 1 -> 1 bed : 80cm, 1 bed : 90cm
Chambre 2 -> 1 bed : 120 cm
Chambre 3 -> 1 bed : 160cm