0

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!

0

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

items is defined other block. So you cannot call it without that block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.