Is it possibile to use an inline return with javascript map function?
Instead of doing
array.map(token => { var x=new Object(); x[token]=words[token]; return x;} )
I would like to do it inline as doing
array.map(token => token )
so applying a inline method like
array.map(token => inline_function(token) )
I have tried like
Object.keys(chart).sort((a,b) => words[b]-words[a]).map(token => ( (token) => (new Object())[token]=words[token] )(token) )
but cannot get the return using a anonymous call with ().
Here is an example using the non inline case:
text = "Lorem ipsum dolor sit amet,\n consectetur adipiscing elit,\nsed do eiusmod tempor incididunt\nut labore et dolore magna aliqua.\nUt enim ad minim veniam,\nquis nostrud exercitation ullamco laboris nisi\nut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit in voluptate velit esse\ncillum dolore eu fugiat nulla pariatur.\nExcepteur sint occaecat cupidatat non proident,\nLorem ipsum dolor sit amet etwas,\nsunt in culpa qui officia deserunt mollit anim id est laborum"
words = text.split(/\s+/g)
count = words.reduce(function(m, v) {
m[v] = m[v] ? m[v] + 1 : 1;
return m;
}, {})
sorted = Object.keys(count).sort((a, b) => count[b] - count[a]).map(token => {
var x = new Object();
x[token] = count[token];
return x;
})
console.log(sorted)