This is best explained by example. The following works in es6 to create an object consisting of some of the keys of an existing object:
var o = {a:1, b: 2, c: 3}
var {a, c} = o
var subsetObj = {a, c} // will be: {a:1, c:3}
There are two downsides here:
- It took two statments, and two lines, to create the desired subset object
- We had to pollute the local variable scope by creating the intermediary
a
andc
variables, which aren't needed locally, except as a means to creating our subset object.
Is there a way to accomplish the same thing in a single statement, without introducing the unnecessary locals a
and c
?