是否存在用于将对象属性的子集复制到新对象中的ES6速记形式? [英] Does an ES6 shorthand exist for copying a subset of an object's properties into a new object?

查看:50
本文介绍了是否存在用于将对象属性的子集复制到新对象中的ES6速记形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下对象:

const obj = {a: 1, b: 2, c: 3, d: 4, e: 5}

是否有用于创建包含以下内容的新对象的简单语法:

Is there a simple syntax for creating a new object that contains:

const obj2 = {a, b, d}

我知道下划线&lodash有 .pick(),但是我希望有一些我可能不知道的破坏性技巧.

I'm aware that underscore & lodash have .pick(), but I'm hoping there's some kind of destructuring trickery that I may not be aware of.

推荐答案

ES2015中简洁的一线式是

Concise one-liner in ES2015 is

const obj2 = (({a, b, d}) => ({a, b, d}))(obj);

似乎不可能避免 {a,b,d} 重言式,同时又要使其紧凑.

It doesn't seem to be possible to avoid {a, b, d} tautology while keeping it compact at the same time.

这篇关于是否存在用于将对象属性的子集复制到新对象中的ES6速记形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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