从箭头函数返回对象 [英] Return object from arrow function

查看:197
本文介绍了从箭头函数返回对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从箭头函数输出对象(一个简短的形式),所以完整的代码是:

I want to output object from arrow function (in a short form), so full code is:

somemethod(function(item) {
   return {id: item.id};
})

与箭头功能是:

somemethod((item) => {
   return {id: item.id};
})

现在的短格式应该是这样的:

and now short form should be something like:

somemethod(item = > {id: item.id} )

这不行,以及这一个:

somemethod(item = > {{id: item.id}} )

现在是使用create Object符号:

only one solution I found for now is to use create Object notation:

somemethod(item = > new Object({id: item.id}) )

有另一种方式吗?

推荐答案

somemethod(item => ({ id: item.id }))

测试:

> a = item => ({id: item.id})
< function item => ({id: item.id})
> a({ id: 5, name: 7 });
< Object {id: 5}

这篇关于从箭头函数返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆