Skip to content

Instantly share code, notes, and snippets.

@SakoMe
Last active February 21, 2018 01:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SakoMe/f64d4a2c22138635e566800a940da9ad to your computer and use it in GitHub Desktop.
Save SakoMe/f64d4a2c22138635e566800a940da9ad to your computer and use it in GitHub Desktop.
Use of advanced reduce method
Mark Williams waffle iron 80 2
Mark Williams blender 200 1
Mark Williams knife 10 4
Nikita Smith waffle iron 80 1
Nikita Smith knife 10 2
Nikita Smith pot 20 3
const fs = require('fs');
let output = fs.readFileSync('data.csv', 'utf8')
.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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment