试图找出是否有办法将某个属性从析构对象分配给一个新变量。我没有在规格中看到任何有用的东西,任何想法?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
首先,假设我将以下常量导入我的文件
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';
为了让我挑选出我想要吃的三明治,我对三明治进行了解构,然后将它们放入一个新的变量三明治IWantToEat。
const { CHEESE_STEAK, SLOPPY_JOE } = SANDWICHES;
const sandwichesIWantToEat = { CHEESE_STEAK, SLOPPY_JOE };
有没有办法去构造,并在一个表达式中重新分配一个变量?
const {
CHEESE_STEAK,
SLOPPY_JOE
}: sandwichesIWantToEat = SANDWICHES;
或者可能是这样的?
const sandwichesIWantToEat = {
CHEESE_STEAK,
SLOPPY_JOE,
...SANDWICHES
};