2

trying to find out if there is a a way to assign certain properties from a destructured object to a new variable. I do not see anything in the specs that would be useful, any ideas?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

First, assume I have the following constant imported to my file

export const SANDWICHES = {
    MEAT_BALL: 'meatball',
    BLT: 'bacon lettuce tomato',
    CHEESE_STEAK: 'cheesesteak',
    HAM_CHEESE: 'ham and cheese',
    SLOPPY_JOE: 'sloppy joe',
    VEGGIE_BURGER: 'veggie'};`

import const { SANDWICHES } from './constants';


In order for me to pick out the sandwiches that I want to eat, I destructure SANDWICHES and then put them into a new variable sandwichesIWantToEat.

const { CHEESE_STEAK, SLOPPY_JOE } = SANDWICHES;
const sandwichesIWantToEat = { CHEESE_STEAK, SLOPPY_JOE };

Is there any way to destructure, and reassign to a variable in a single expression?

const {
    CHEESE_STEAK,
    SLOPPY_JOE
}: sandwichesIWantToEat = SANDWICHES;

Or maybe something like this?

const sandwichesIWantToEat = {
    CHEESE_STEAK,
    SLOPPY_JOE,
    ...SANDWICHES
};
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
Greg
  • 1,005
  • 4
  • 13
  • 29
  • 2
    There should be, but there's not. Two proposed syntaxes are (1) `const sandwichesIWantToEat = SANDWICHES.{ CHEESE_STEAK, SLOPPY_JOE };`, and (2) `const sandwichesIWantToEat = { {CHEESE_STEAK, SLOPPY_JOE} = SANDWICHES };`. This has been discussed on the [ES Discuss](https://esdiscuss.org/topic/extended-dot-notation-pick-notation-proposal) mailing list, but the TC39 folks seem pre-occupied with more arcane matters. –  Sep 20 '16 at 19:35
  • thanks, appreciate answer – Greg Sep 20 '16 at 19:46
  • I feel like this question is asked every other day :P – Felix Kling Sep 20 '16 at 20:42

0 Answers0