I am iterating over an table to pick out the form elements from each table row. What am I doing wrong in the below example?
var result = new Array();
var counter = 1;
$('tbody tr', this.el).each(function(){
var inner = new Array();
$('input',this).each(function(){
console.log(this.name, $(this).val()); // Works: sends name/value to console!
inner[this.name] = $(this).val(); // Appears to be empty
});
result[counter] = inner;
counter++;
});
console.log(result);
You’re doing arrays wrong.
In Javascript, an Array is a special case of object which has numeric keys only and has an interface defined by functions like
push:You’re making the very common mistake of trying to use an array like a more generic object, assigning string keys and using
x[y] = znotation in order to do so. It’s a hack that sometimes may happen to seem to work, but is not how arrays are supposed to be used.You have the same problem with
innerand withresult.Instead, use the generic object that you were attempting to use in the first place: