-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmapReduceFaker.js
More file actions
39 lines (32 loc) · 1.01 KB
/
Copy pathmapReduceFaker.js
File metadata and controls
39 lines (32 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Example of Map-Reduce on a generated DataSet with faker
// Use npm init on new project folder
// Then npm i faker --save to install faker
// Copy this file on project folder as index.js
const faker = require('faker')
const orders = []
for (let i = 0; i < 1000; i++) {
orders.push({
name: faker.name.findName(),
city: faker.address.city(),
zipCode: faker.address.zipCode(),
product: faker.commerce.productName(),
price: parseInt(faker.commerce.price(), 10)
})
}
const directionCityOrders = orders.filter(order => order.city.match(/^(North|South|West|East).*/))
const cityDirectionsOnlyOrders = directionCityOrders.map(order => {
let direction = order.city.split(" ")[0]
return {
city: direction,
price: order.price
}
})
const groupCount = cityDirectionsOnlyOrders.reduce((count, order) => {
let city = order.city
if (count[city])
count[city]++
else
count[city] = 1
return count
}, [])
console.log(groupCount)