# Destructuring - Assign selected props to a new variable

**URL:** <https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [November 3, 2019, 1:21pm UTC](https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118 "2019-11-03T13:21:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yinon](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/yinon/32/331_2.png) [@yinon](https://es.discourse.group/u/yinon)\
**Post date:** [November 3, 2019, 1:21pm UTC](https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118/1 "2019-11-03T13:21:50Z")

</div>

I often run into this scenario where in need of a portion of an object. destructuring surely helps in grabbing what is needed. the annoyance occur when the portion is used as a new object - forcing the code to like this:

```javascript
const {b, d} = {a: 10, b: 20, c: 30, d: 40}
someFunction({b,d});

```

this is a duplication of the properties and is a pitfall in some cases where you might leave out few properties unwillingly (when it comes to large portions from large objects).

a syntax such as the following might achieve better stability, assurance and nonrepetitive code.

```javascript
const {b, d} as newObj = {a: 10, b: 20, c: 30, d: 40}

```

it's currently possible to gather the rest of unmentioned properties with _{...rest}_ but I think in most cases it is way more essential to select and group the destructured properties.

---

<div class="post-metadata">

**Author:** ![mikesamuel](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mikesamuel/32/137_2.png) [@mikesamuel](https://es.discourse.group/u/mikesamuel)\
**Post date:** [November 15, 2019, 8:28pm UTC](https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118/2 "2019-11-15T20:28:55Z")

</div>

Are you familiar with the ["as patterns"](https://github.com/zkat/proposal-as-patterns) proposal?

IIRC, that's part of zkat's larger [pattern matching](https://github.com/tc39/proposal-pattern-matching/blob/5b5c50faa40d6a8a19a5df55fb2f90fab476a582/CORE.md) proposal.

---

<div class="post-metadata">

**Author:** ![yinon](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/yinon/32/331_2.png) [@yinon](https://es.discourse.group/u/yinon)\
**Post date:** [November 19, 2019, 1:10pm UTC](https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118/3 "2019-11-19T13:10:26Z")

</div>

Seems just right. but this proposal is 2 years old and there's no evidence of progress 😕  
it is also really similiar and covers more patterns.

I'd vote for it!

---

<div class="post-metadata">

**Author:** ![mikesamuel](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mikesamuel/32/137_2.png) [@mikesamuel](https://es.discourse.group/u/mikesamuel)\
**Post date:** [November 19, 2019, 6:30pm UTC](https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118/4 "2019-11-19T18:30:41Z")

</div>

Unfortunately, zkat is [not able to help with TC39 anymore](https://hub.packtpub.com/npm-inc-after-a-third-try-settles-former-employee-claims-who-were-fired-for-being-pro-union-the-register-reports/) through no fault of her own.

If someone did want to pick that up though, they could build on her work.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [November 19, 2019, 8:17pm UTC](https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118/5 "2019-11-19T20:17:59Z")

</div>

zkat's current employer is an ecma member, so there's no barrier to their participation.

---

<div class="post-metadata">

**Author:** ![Maxwell-Alexius](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/maxwell-alexius/32/145_2.png) [@Maxwell-Alexius](https://es.discourse.group/u/Maxwell-Alexius)\
**Post date:** [January 14, 2020, 9:36am UTC](https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118/6 "2020-01-14T09:36:29Z")

</div>

Hi, I've came across this issue and I'm also interested in it. Is there any way I can start help contribute to this proposal? Maybe I can help draft the stage-0 version of the as-proposal which is based on zkat's version.

Thanks!

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [January 14, 2020, 11:11pm UTC](https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118/7 "2020-01-14T23:11:48Z")

</div>

I think the [_property shorthand improvements_ proposal](https://github.com/rbuckton/proposal-shorthand-improvements) would have a much better chance at adoption, and is much more useful.

The syntax to achieve the goal from the OP's example:

```javascript
const obj = {a: 10, b: 20, c: 30, d: 40}
someFunction({obj.b, obj.d});

```

---

<div class="post-metadata">

**Author:** ![yinon](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/yinon/32/331_2.png) [@yinon](https://es.discourse.group/u/yinon)\
**Post date:** [June 3, 2020, 9:41pm UTC](https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118/8 "2020-06-03T21:41:06Z")

</div>

wanna collaborate on this?

---

<div class="post-metadata">

**Author:** ![JhonnattanRivera](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jhonnattanrivera/32/409_2.png) [@JhonnattanRivera](https://es.discourse.group/u/JhonnattanRivera)\
**Post date:** [August 21, 2020, 7:13am UTC](https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118/9 "2020-08-21T07:13:56Z")

</div>

|FRIEND I love your proposal is exactly what I have always needed.

`const {b=1, d, e = 50} as newObj = {a: 10, b: 20, c: 30, d: 40}`

i like this

`console.log(newObject) // { b:20, d:40, e:50 }`

---

<div class="post-metadata">

**Author:** ![seekr3](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/seekr3/32/476_2.png) [@seekr3](https://es.discourse.group/u/seekr3)\
**Post date:** [November 5, 2020, 6:17pm UTC](https://es.discourse.group/t/destructuring-assign-selected-props-to-a-new-variable/118/10 "2020-11-05T18:17:32Z")

</div>

I like this. Wish there was an easier way to do that.
