How to make an array of arrays like
{ [1,2,3],[2,3,4],[2,34,55] }
in jQuery ?
$(document).ready(function() {
var row = 4;
var items = [];
var total = [];
$('#test tr:eq(' + row + ') td').each(function(colindex, col) {
//alert(colindex);
t = $(this).contents();
items.length = 0;
$.each(t, function(i, val) {
if (val.tagName != 'BR') {
if (val.innerHTML == undefined) {
items.push(val.data);
}
else
items.push(val.innerHTML);
}
//alert(items.toString());
});
total.push(items);
});
alert(total.toString());
});
iIn the above code I’m trying to create a array Total() with the elements as arrays (item()), but how ever the Total() array has only one object that too the last item() array.
The problem is that you’re reusing the same
itemsarray on each loop. Instead, create a newitemsarray:When you set the
lengthproperty of an array to0, you’re removing all of its array elements but it’s still the same array, so you ended up pushing the same array onto yourtotalsarray repeatedly.