这应该有效:
$(window).load(function() {
var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
var productMonthMappings = [];
$.each($('.single-record .product'), function(index, item) {
// get productId
var productId = $(item).data('record');
// Create month mappings for this product if not existent
if (typeof productMonthMappings[productId] === 'undefined'
|| productMonthMappings[productId] === null) {
productMonthMappings[productId] = [];
for (var i = 0; i < 12; i++) {
// Set initial count to 0
productMonthMappings[productId][i] = 0;
}
}
// Parse the date and get the month for the current record
var date = new Date(1000 * $(item).next().html());
var month = date.getMonth();
var monthName = months[month];
// Increment the count for that date for the product
productMonthMappings[productId][month]++;
});
// Print counts per month
$.each(productMonthMappings, function(i, productMonthMapping){
if (productMonthMapping !== undefined
&& productMonthMapping !== null
&& $('#product-id-'+i).length > 0) {
$.each(productMonthMapping, function(j, count){
if (count === undefined || count === null) {
count = 0;
}
$('#product-id-'+i).append(
' | ' + months[j] + '(' + count + ')'
);
});
}
});
});
小提琴:http: //jsfiddle.net/pjyb8/