Skip to content

Instantly share code, notes, and snippets.

@somiandras
Last active November 7, 2016 17:27
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 somiandras/bbef105f79168baf9837fad86c10dd8f to your computer and use it in GitHub Desktop.
Save somiandras/bbef105f79168baf9837fad86c10dd8f to your computer and use it in GitHub Desktop.
Map and reduce instead of entangled 'for..each' and 'if...else' blocks.
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);
}
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);
}
@gyurisc
Copy link

gyurisc commented Nov 7, 2016

looks really neat... :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment