Last active
November 7, 2016 17:27
-
-
Save somiandras/bbef105f79168baf9837fad86c10dd8f to your computer and use it in GitHub Desktop.
Map and reduce instead of entangled 'for..each' and 'if...else' blocks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function extractData(messages) { | |
let data = {}; | |
messages.forEach(message => { | |
if (message.symbols) { | |
let symbols = message.symbols; | |
symbols.forEach(symbol => { | |
if (data[symbol.symbol]) { | |
data[symbol.symbol].count += 1; | |
} else { | |
data[symbol.symbol] = { | |
count: 1, | |
title: symbol.title | |
}; | |
} | |
}); | |
} | |
}); | |
return arrayify(d); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function extractData(messages) { | |
let data = messages | |
.map(m => m.symbols) | |
.reduce((result, curr) => result.concat(curr), []) | |
.reduce((result, curr) => { | |
let ticker = curr.symbol; | |
result[ticker] = result[ticker] || {title: curr.title, count: 0}; | |
result[ticker].count += 1; | |
return result; | |
}, {}); | |
return arrayify(data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
looks really neat... :)