0

What is the ES6 (ES2015) way to create new object from particular keys without repeat the keys twise.

for example:

var orginalObject= {a:1,b:2,c:3,d:4}
// not working
var extractedObj = { orginalObject.a , orginalObject.b} // extractedObj == {a:1, b:2}

I ask that because EC6 as a lot of new shiny ways to copy and create objects

pery mimon
  • 5,635
  • 4
  • 37
  • 45
  • 1
    It is simple enough already. Nothing special in ES2015 that could make it more concise (in terms of characters or statements number). – zerkms Dec 13 '16 at 02:08
  • What made you think ES6 would offer anything to "simplify" that? – Felix Kling Dec 13 '16 at 02:40
  • @FelixKling Although not mentioned at within text of Question, OP is getting syntax error using pattern at Question, no? – guest271314 Dec 13 '16 at 02:43
  • @guest271314: That's correct. – Felix Kling Dec 13 '16 at 02:45
  • @FelixKling Utilizing default value `var {newObj = {a:oldObj.a, b:oldObj.b}} = {}` http://stackoverflow.com/a/41113795/ – guest271314 Dec 13 '16 at 04:33
  • 1
    @guest271314: Why would that be any better/simpler than doing `var newObj = {a:oldObj.a, b:oldObj.b};` directly? To me it's the opposite. – Felix Kling Dec 13 '16 at 05:13
  • @FelixKling Good point. Does the same hold for linked Question? Or, is there a different expectation there? – guest271314 Dec 13 '16 at 05:17
  • 1
    @FelixKling Re-marked the Question as duplicate too prematurely, here. The difference between the two Questions: _"How one can write a function"_ http://stackoverflow.com/questions/25553910/one-liner-to-take-some-properties-from-object-in-es-6 . If the present Question is a duplicate, it is not of the linked Question. – guest271314 Dec 13 '16 at 06:24
  • @guest271314 it is much more simpler when key's object is too long. like in my real use case : `{startAnimation:orginal.startAnimation, endAnimation:orginal.endAnimation)` instead of (for example):`{orginal.startAnimation,orginal.endAnimation)` – pery mimon Dec 13 '16 at 13:46
  • @FelixKling because the EC6 provide a lot of new shiny ways to copy and move properties from objects and arrays – pery mimon Dec 13 '16 at 14:03
  • @perymimon _"it is much more simpler when key's object is too long"_ What do you mean by "it"? Not sure what you are trying to convey? – guest271314 Dec 13 '16 at 16:36
  • @guest271314 not repeat the keys twise . to define the key and to extract it from the orginal object – pery mimon Jan 08 '17 at 15:31
  • @perymimon Does Answer by gyre not return expected result? See http://stackoverflow.com/help/someone-answers. _"not repeat the keys twise . to define the key and to extract it from the orginal object"_ Though not sure if the Question has not been previously asked, have you considered asking a new Question specific to not repeating keys? The present Question does not contain text "not repeat the keys twise"; which should also be clearly defined within the text body of the Question itself. – guest271314 Jan 08 '17 at 17:46
  • @guest271314 thank, point taken – pery mimon Jan 12 '17 at 18:32

1 Answers1

3

You could make it really sharp and ES6-y with multiple let statements.

let oldObj = {a:1, b:2, c:3, d:4},
    newObj
{
    let {a, b} = oldObj
    newObj = {a, b}
}

console.log(newObj) //=> { a:1, b:2 }

Or just opt for the simpler ES5 version:

var oldObj = {a:1, b:2, c:3, d:4}
var newObj = {a: oldObj.a, b: oldObj.b}

console.log(newObj) //=> { a:1, b:2 }
gyre
  • 14,437
  • 1
  • 32
  • 46
  • 1
    I just fixed that wording, you read my mind. "Sharp and ES6-y" is a better description. Maybe "edgy" would be best. – gyre Dec 13 '16 at 02:11
  • It will however pollute the current scope. – Felix Kling Dec 13 '16 at 02:41
  • it nice, but that is the best way to achieve that ? – pery mimon Dec 13 '16 at 13:56
  • @perymimon What is the definition of "best" as applied in this case? Which options are you comparing when evaluating which is "best"? How can another user determine what is "best" for your "real use case" where `javascript` representing the real use case does not appear at original Question? – guest271314 Dec 13 '16 at 16:45
  • @perymimon: These are the two best ways I can think of. Honestly, I would probably choose the second; using ES6 object destructuring doesn't seem to make your resulting code much terser or more readable. – gyre Dec 13 '16 at 19:49