Arrow Function example

In this example we have a array of car objects and the goal is to take all the ford cards and add their km together

const cars = [
 {brand: 'Volvo', km: 20000},
 {brand: 'Ford', km: 60000},
 {brand: 'Toyota', km: 50000},
 {brand: 'Ford', km: 50000},
 {brand: 'Ford', km: 70000},
 {brand: 'Ferrari', km: 20000}
]

const reduceTotal = (prev, x) => (prev || 0 ) + x;
const totalFordsKm = cars;

.filter(event => event.brand === 'Ford')
.map(event => event.km)
.reduce(reduceTotal);
 
console.log(totalFordsKm);

Add your comment