Brendan Gooden Brendan Gooden - 5 months ago 11
Javascript Question

Jquery add data array to table rows

I have a Jquery code to loop through a table and select unique values, then populate another table with these unique values. My code is as follows:

var items = [], options = [];

//Iterate all td's in first column
$('#PartsTable tr td:nth-child(1)').each(function () {
//add item to array
items.push($(this).text());
});

//restrict array to unique fleet numbers
var items = $.unique(items);
});

//iterate unique array and build array of select options
// ERROR HERE - 'ITEMS IS UNDEFINED'
$.each(items, function(i, item) {
options.push('<tr><td align="left" valign="top">' + item + '</td><td class="delete" align="center" style="background-color: transparent;"><i class="fa fa-times text-red cur-pon"></i></td></tr>');
});

//finally append the items from the array
$('#OrderSummaryTbody').append(options.join());


However I'm getting an error on the line marked above - 'items is undefined'

When I run the debugger in Chrome,
var items = $.unique(items);
clearly returns an array with 2 values in it.

Can anyone shed any light on this? Thanks!

Answer

There is a typo here

var items = $.unique(items);
});

That last }); shouldn't be here, OR, this is inside a function so that items var is not global so the other code can't ready it.

It would be better if you can show the code before that to see the complete function.