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 modo 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)
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 modo 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)
Share
Improve this question
asked Nov 12, 2018 at 14:26
loretoparisiloretoparisi
16.3k12 gold badges112 silver badges158 bronze badges
0
2 Answers
Reset to default 5All of those are inline functions. Fundamentally, you're asking how to write this callback:
token => {
var x = new Object();
x[token] = count[token];
return x;
}
with a concise body rather than a function body. In this specific case, you can do that with a puted property name:
token => ({[token]: count[token]})
// ^^^^^^^---- puted property name, new in ES2015
Note we need the parens (()
) around the concise body because the first character of it would be {
(the beginning of the object initializer) otherwise, which would make the parser think it had a function body rather than a concise one.
In the more general case, you can't turn a function body into a concise body unless everything fits within a single expression, such as the object initializer above. Sometimes you can use (or arguably abuse) the ma operator for that, with the left-hand operand to ,
having side-effects, but in such cases it's usually clearer to just leave the function body instead.
As an example of that (ab)use, this logs the values prior to doubling them:
doubled = values.map(v => (console.log(v), v * 2));
I don't remend it, but you'll see it done. :-)
Why not pass a function to .map
?
function myFunction(token){
var x = new Object();
x[token] = count[token];
return x;
}
sorted = Object.keys(count).sort((a, b) => count[b] - count[a]).map(myFunction);