Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var cart = [{ productName: "Fish", quantity: 4, cost: 10.40},
            { productName: "Beef", quantity: 2, cost: 5.50},
            { productName: "Green stuff", quantity: 25, cost: 0.75}];
// Get extended line items => { productName: "XXX", totalCost: XX.XX}
var lineItems = [];
for ( var i = 0; i < cart.length; i++) {
  var line = { productName: cart[i].productName,
               totalCost: cart[i].quantity * cart[i].cost};
  lineItems.push( line);
}
console.log( lineItems);
var total = 0;
for ( var i = 0; i < lineItems.length; i++) {
  total += lineItems[i].totalCost;
}
console.log( total);
var easierLines = _.map(cart, function(f) {
  return { productName: f.productName, total: f.cost * f.quantity};
});
var easierTotal = _.reduce(easierLines, function(sum, current) {
  return sum + current.total;
}, 0);
console.log( easierTotal);
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
meilingerpro
0viewers