Sort-Multiply Array Table
by Jose Montero
HTML
<table>
<tbody>
<tr>
<th>Item</th><th>Price</th><th>Qty</th><th>Ext Price</th>
</tr>
</tbody>
</table>
CSS
table, th, td {
border: 1px solid black;
}
th{
font-weight: bold;
text-align: center;
}
JavaScript
$(function(){
var itemsArray = [
{ 'item' : 'Hammer', 'Price' : 15.00, 'qty' : 5},
{ 'item' : 'Drill', 'Price' : 49.99, 'qty' : 3},
{ 'item' : 'Screwdriver', 'Price' : 3.00, 'qty' : 6},
{ 'item' : 'Wrench', 'Price' : 6.25, 'qty' : 4},
{ 'item' : 'Pliers', 'Price' : 4.00, 'qty' : 2},
{ 'item' : 'Ratchet', 'Price' : 12.50, 'qty' : 1},
{ 'item' : 'Chisel', 'Price' : 8.50, 'qty' : 7},
{ 'item' : 'Prybar', 'Price' : 7.25, 'qty' : 4},
{ 'item' : 'Saw', 'Price' : 14.50, 'qty' : 6},
{ 'item' : 'Sander', 'Price' : 27.99, 'qty' : 2}
];
var resultArray = itemsArray.map((item) =>{ return {'item' : item.item, 'price' : item.Price, 'qty' : item.qty, 'extprice' : item.qty * item.Price} })
function sortArray(a, b)
{
var itemA = a.item.toUpperCase(); // ignore upper and lowercase
var itemB = b.item.toUpperCase(); // ignore upper and lowercase
return (itemA < itemB) ? -1 : ((itemA > itemB) ? 1 : 0);
}
resultArray.sort(sortArray).forEach((item) =>{
$('table tbody').append(`<tr><td>${item.item}</td><td>${item.price}</td><td>${item.qty}</td><td>${item.extprice}</td></tr>`)
})
})