I was just searching for a simple jquery sorting script and found this online:
$(function() {
$('ol').each(function() {
var matches = $('li', this).filter(function() { // Each item
var text = $(this).text();
return $('ul.three li').filter(function() { // Are there matches in .three?
return $(this).text() == text;
}).length > 0;
}).length;
$('li:last', this).text((matches * 10) + ' Points');
});
});
Notice how the values of the first 2 tables are compared with that of the values of the third table. now my question is about how matches is incremented, i went through the whole script, but don't quite understand how matches gets a value of 4 and 2 for the first and second table respectively(displayed in the respective table as 40 and 20). I understand the usage of the filter function, but don't quite understand how matches gets its value, I know the length property is involved, i see it at the end of the return statement, But still don't quite understand how is matches incremented ?
filterreturns the elements that matched the filter, basically, and thenlengthreturns the size of the items array.. What is it exactly what you don't understand?liin thatoland compares each of them to see if it's texts is in any of theli's in.three.. And as you can see, there are four. So the filter returns that fourlielements in an array (not reaaly an array, but a "jQuery array") and then, thelengthis 4.