javascript - Doesn't the simplest form of arrow function return an object? map(i => { name: i})
黄舟
黄舟 2017-06-14 10:53:09
0
2
1137

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]
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
phpcn_u1582

Definitely cannot use {} directly, because the function body is also implemented using {}, which will be considered to have an a:1expression

in the function body.

The simplest way to write it is to wrap it with ()

arr.map(i => ({a: i}))
阿神
arr.map(i => ({ a: i})) 
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template