If the arrow function does not use curly braces{}
, it is equivalent to returning directly
const arr = [1, 2, 3, 4] arr.map(i => i) // 1 ,2 ,3, 4
But if I want to return an object, I cannot use the simplest form?
arr.map(i => { a: i}) // [undefined, undefined, undefined, undefined]
Must be enclosed in curly braces
arr.map(i => return {{ a: i}}) // [Object, Object, Object, Object]
Definitely cannot use
in the function body.{}
directly, because the function body is also implemented using{}
, which will be considered to have ana:1
expressionThe simplest way to write it is to wrap it with
()