I have this set of data pulled from the database. The prices are different but some data are redundant.
Here's the screenshot of the data.
The table that I want it to look like is this.
I arranged them in jQuery using .each() but what I got is an epic fail.
Only the header and first body row is correct. Here's my code:
var route_price_table = '';
route_price_table = '<table class="table table-bordered table-striped"><thead><tr><th>SEA</th>';
var initial_boxname = '';
var iterator = 0;
var iterator2 = 0;
var length = data.length;
var route_price_body_table = '';
var route_price_body_table2= '';
var initial_routename = '';
$.each( data, function( key, value ) {
if(iterator == key){
initial_routename = value.Route;
route_price_table += '<th>' + value.BoxName + '</th>';
initial_boxname = value.BoxName;
if(initial_routename == value.Route){
route_price_body_table += '<tr><td>' + value.Route + '</td><td>' + value.Price + ' (' + value.BoxName + ')</td>';
}
}
if(initial_boxname != value.BoxName){
initial_boxname = value.BoxName;
route_price_table += '<th>' + value.BoxName + '</th>';
route_price_body_table += '<td>' + value.Price + ' (' + value.BoxName + ')</td>';
}
if(initial_routename != value.Route){
route_price_body_table2 += '<tr><td>' + value.Route + '</td><td>' + value.Price + ' (' + value.BoxName + ')</td>';
initial_routename = value.Route;
}
if (key === (length - 1)) {
route_price_table += '</tr></thead><tbody>';
route_price_table += route_price_body_table + route_price_body_table2;
}
});
route_price_table += '</tbody></table>';
$('#route_price_table').html(route_price_table);
Can this be achieved using jQuery or do I need to just arrange it backend? Any help is highly appreciated.

jQuerybut I'll suggest you to do it onbackendso that you don't have to traverse all data again and again.