Skip to content

Instantly share code, notes, and snippets.

@garrettmac
Last active September 15, 2017 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garrettmac/b5c2e42388a5a1236e4ef93c166908f7 to your computer and use it in GitHub Desktop.
Save garrettmac/b5c2e42388a5a1236e4ef93c166908f7 to your computer and use it in GitHub Desktop.
MEDIUM BLOG POST - Javascript’s 3 Major Paradigms: The Five tenets of Functional Programming [part 3 of 4]
const vehicles = [
 { make: ‘Honda’, model: ‘CR-V’, type: ‘suv’, price: 24045 },
 { make: ‘Honda’, model: ‘Accord’, type: ‘sedan’, price: 22455 },
 { make: ‘Mazda’, model: ‘Mazda 6’, type: ‘sedan’, price: 24195 },
 { make: ‘Mazda’, model: ‘CX-9’, type: ‘suv’, price: 31520 },
 { make: ‘Toyota’, model: ‘4Runner’, type: ‘suv’, price: 34210 },
 { make: ‘Toyota’, model: ‘Sequoia’, type: ‘suv’, price: 45560 },
 { make: ‘Toyota’, model: ‘Tacoma’, type: ‘truck’, price: 24320 },
 { make: ‘Ford’, model: ‘F-150’, type: ‘truck’, price: 27110 },
 { make: ‘Ford’, model: ‘Fusion’, type: ‘sedan’, price: 22120 },
 { make: ‘Ford’, model: ‘Explorer’, type: ‘suv’, price: 31660 }
];
let Fords=vehicles.filter(x=>x.make===”Ford”)
console.log(Fords) // [{‘make’:’Ford’,’model’:’F-150',’type’:’truck’,’price’:27110},{‘make’:’Ford’,’model’:’Fusion’,’type’:’sedan’,’price’:22120},{‘make’:’Ford’,’model’:’Explorer’,’type’:’suv’,’price’:31660}]”
//or
const averageSUVPrice = vehicles
 .filter(v => v.type === ‘suv’)
 .map(v => v.price)
 .reduce((sum, price, i, array) => sum + price / array.length, 0);
console.log(averageSUVPrice); // 33399
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment