I am inserting some data into an array but I am getting
,9,My firstname,My lastname,myemail@example.com,123456789
out in my console. How can I remove comma from first element i.e. 9? here is my code
var data = new Array();
$(row_el.children("td")).each(function(i) {
var td = $(this);
//var data = td.html();
var td_el = td.attr('class');
//arr[i] = data;
if(!td.hasClass("element")) {
data[i] = td.html();
console.log(i + ": " + data);
}
});
I want output like this
9,My firstname,My lastname,myemail@example.com,123456789
then I will pass this array to a function and loop through this array.
Just use
push()(MDN docu) instead of setting to a specific position inside your array:The problem is, that not all the elements in the array processed by
each()result in an entry in your array. However, the index submitted to your callback (i) counts upwards nonetheless. So if there are some elements, that do not result in an entry to your array, these positions are filled withundefined(as long as you enter an element at a later position). Theseundefinedentries now result in an empty string when outputting it the way you do, thus resulting to your leading comma.