forked from nhattruongniit/learn-javascripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce-advance.js
More file actions
58 lines (49 loc) · 896 Bytes
/
reduce-advance.js
File metadata and controls
58 lines (49 loc) · 896 Bytes
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* Bai tap
input: from 1 file.txt
truong knife 90 2
truong pot 200 3
nguyen waffte 120 6
nguyen fish 150 7
output:
{
"truong": [
{
"name": knife,
"price": "90",
"quanlity": "2"
},
{
"name": pot,
"price": "200",
"quanlity": "3"
}
],
"nguyen": [
{
"name": waffte,
"price": "120",
"quanlity": "6"
},
{
"name": fish,
"price": "150",
"quanlity": "7"
}
],
}
*/
var fs = require('fs');
var output = fs.readFileSync('data/data-reduce.txt', 'utf-8')
.trim()
.split('\n')
.map(line => line.split(' '))
.reduce((customers, line) => {
customers[line[0]] = customers[line[0]] || [];
customers[line[0]].push({
name: line[1],
price: line[2],
quantity: line[3]
})
return customers
}, {})
console.log('output: ', JSON.stringify(output, null, 2));