Destructing partial object to a new object in js
up vote
0
down vote
favorite
Given the following object {a:1, b:2, c:3} I'm looking for es6 syntax to distruct a subset of the object to a new one i.e: {b:2 ,c:3}.
I've found this syntax enabling me to omit the properties I don't want:
const {a,...newObj} = original.
How can I do it, with including the properties names.
javascript
add a comment |
up vote
0
down vote
favorite
Given the following object {a:1, b:2, c:3} I'm looking for es6 syntax to distruct a subset of the object to a new one i.e: {b:2 ,c:3}.
I've found this syntax enabling me to omit the properties I don't want:
const {a,...newObj} = original.
How can I do it, with including the properties names.
javascript
Object spread/rest is not a part of the language yet, it will probably be included in ES 2018 though. There's not a easy syntax to do it.
– Axnyff
Jan 10 at 10:57
Cool, but I'm working with Babel
– Roni Gadot
Jan 10 at 11:00
@RoniGadot yourConst {a,...newObj} = originalworks fine. Do you want to achieve something else. Please explain
– Rohit Agrawal
Jan 10 at 11:03
It works, but I want to cherry pick the fields I want and not omit the ones I don't
– Roni Gadot
Jan 10 at 11:10
@RoniGadot, added my answer according to your requirement. Please let me know in case of some problem.
– Rohit Agrawal
Jan 10 at 11:23
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Given the following object {a:1, b:2, c:3} I'm looking for es6 syntax to distruct a subset of the object to a new one i.e: {b:2 ,c:3}.
I've found this syntax enabling me to omit the properties I don't want:
const {a,...newObj} = original.
How can I do it, with including the properties names.
javascript
Given the following object {a:1, b:2, c:3} I'm looking for es6 syntax to distruct a subset of the object to a new one i.e: {b:2 ,c:3}.
I've found this syntax enabling me to omit the properties I don't want:
const {a,...newObj} = original.
How can I do it, with including the properties names.
javascript
javascript
edited Jan 10 at 14:38
Shlomi Schwartz
3,0392071113
3,0392071113
asked Jan 10 at 10:55
Roni Gadot
396516
396516
Object spread/rest is not a part of the language yet, it will probably be included in ES 2018 though. There's not a easy syntax to do it.
– Axnyff
Jan 10 at 10:57
Cool, but I'm working with Babel
– Roni Gadot
Jan 10 at 11:00
@RoniGadot yourConst {a,...newObj} = originalworks fine. Do you want to achieve something else. Please explain
– Rohit Agrawal
Jan 10 at 11:03
It works, but I want to cherry pick the fields I want and not omit the ones I don't
– Roni Gadot
Jan 10 at 11:10
@RoniGadot, added my answer according to your requirement. Please let me know in case of some problem.
– Rohit Agrawal
Jan 10 at 11:23
add a comment |
Object spread/rest is not a part of the language yet, it will probably be included in ES 2018 though. There's not a easy syntax to do it.
– Axnyff
Jan 10 at 10:57
Cool, but I'm working with Babel
– Roni Gadot
Jan 10 at 11:00
@RoniGadot yourConst {a,...newObj} = originalworks fine. Do you want to achieve something else. Please explain
– Rohit Agrawal
Jan 10 at 11:03
It works, but I want to cherry pick the fields I want and not omit the ones I don't
– Roni Gadot
Jan 10 at 11:10
@RoniGadot, added my answer according to your requirement. Please let me know in case of some problem.
– Rohit Agrawal
Jan 10 at 11:23
Object spread/rest is not a part of the language yet, it will probably be included in ES 2018 though. There's not a easy syntax to do it.
– Axnyff
Jan 10 at 10:57
Object spread/rest is not a part of the language yet, it will probably be included in ES 2018 though. There's not a easy syntax to do it.
– Axnyff
Jan 10 at 10:57
Cool, but I'm working with Babel
– Roni Gadot
Jan 10 at 11:00
Cool, but I'm working with Babel
– Roni Gadot
Jan 10 at 11:00
@RoniGadot your
Const {a,...newObj} = original works fine. Do you want to achieve something else. Please explain– Rohit Agrawal
Jan 10 at 11:03
@RoniGadot your
Const {a,...newObj} = original works fine. Do you want to achieve something else. Please explain– Rohit Agrawal
Jan 10 at 11:03
It works, but I want to cherry pick the fields I want and not omit the ones I don't
– Roni Gadot
Jan 10 at 11:10
It works, but I want to cherry pick the fields I want and not omit the ones I don't
– Roni Gadot
Jan 10 at 11:10
@RoniGadot, added my answer according to your requirement. Please let me know in case of some problem.
– Rohit Agrawal
Jan 10 at 11:23
@RoniGadot, added my answer according to your requirement. Please let me know in case of some problem.
– Rohit Agrawal
Jan 10 at 11:23
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
You can cherry pick the properties you want like shown below. The name of the new variable should be same as the property name. Then you can create a new object containing those picked properties.
var obj = {a:1, b:2, c:3};
var {b, c} = obj;//pick property b and c
var newObj = {b, c}; //shortcut for {b:b, c:c} in ES-6
console.log(newObj);add a comment |
up vote
2
down vote
I prefer doing the following
( ({key1, key2}) => ({ key1, key2 }) )(obj);
It allows me getting a new object without creating intermediate variables or objects.
For example -
var obj = {a:1, b:2, c:3};
var newObj = ( ({a, b}) => ({ a, b }) )(obj);
console.log(newObj);add a comment |
up vote
1
down vote
I'm not sure what you really want. But as far as I understood this will help you.
For example
I have the initial object
{ a: 10, b: 20, c: 100 }
Properties I want to extract are a, b
And I want to create new object c with property b, c
Here is how it is done. Using destructive assignment. Here the out put will be 20 as in the initial object.
({ a, b } = { a: 10, b: 20, c: 100 });
let c = {a: a, b: b};
console.log(c.b);MDN Document to destructive assignment. link
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can cherry pick the properties you want like shown below. The name of the new variable should be same as the property name. Then you can create a new object containing those picked properties.
var obj = {a:1, b:2, c:3};
var {b, c} = obj;//pick property b and c
var newObj = {b, c}; //shortcut for {b:b, c:c} in ES-6
console.log(newObj);add a comment |
up vote
2
down vote
accepted
You can cherry pick the properties you want like shown below. The name of the new variable should be same as the property name. Then you can create a new object containing those picked properties.
var obj = {a:1, b:2, c:3};
var {b, c} = obj;//pick property b and c
var newObj = {b, c}; //shortcut for {b:b, c:c} in ES-6
console.log(newObj);add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can cherry pick the properties you want like shown below. The name of the new variable should be same as the property name. Then you can create a new object containing those picked properties.
var obj = {a:1, b:2, c:3};
var {b, c} = obj;//pick property b and c
var newObj = {b, c}; //shortcut for {b:b, c:c} in ES-6
console.log(newObj);You can cherry pick the properties you want like shown below. The name of the new variable should be same as the property name. Then you can create a new object containing those picked properties.
var obj = {a:1, b:2, c:3};
var {b, c} = obj;//pick property b and c
var newObj = {b, c}; //shortcut for {b:b, c:c} in ES-6
console.log(newObj);var obj = {a:1, b:2, c:3};
var {b, c} = obj;//pick property b and c
var newObj = {b, c}; //shortcut for {b:b, c:c} in ES-6
console.log(newObj);var obj = {a:1, b:2, c:3};
var {b, c} = obj;//pick property b and c
var newObj = {b, c}; //shortcut for {b:b, c:c} in ES-6
console.log(newObj);edited Jan 10 at 11:32
answered Jan 10 at 11:20
Rohit Agrawal
1,156315
1,156315
add a comment |
add a comment |
up vote
2
down vote
I prefer doing the following
( ({key1, key2}) => ({ key1, key2 }) )(obj);
It allows me getting a new object without creating intermediate variables or objects.
For example -
var obj = {a:1, b:2, c:3};
var newObj = ( ({a, b}) => ({ a, b }) )(obj);
console.log(newObj);add a comment |
up vote
2
down vote
I prefer doing the following
( ({key1, key2}) => ({ key1, key2 }) )(obj);
It allows me getting a new object without creating intermediate variables or objects.
For example -
var obj = {a:1, b:2, c:3};
var newObj = ( ({a, b}) => ({ a, b }) )(obj);
console.log(newObj);add a comment |
up vote
2
down vote
up vote
2
down vote
I prefer doing the following
( ({key1, key2}) => ({ key1, key2 }) )(obj);
It allows me getting a new object without creating intermediate variables or objects.
For example -
var obj = {a:1, b:2, c:3};
var newObj = ( ({a, b}) => ({ a, b }) )(obj);
console.log(newObj);I prefer doing the following
( ({key1, key2}) => ({ key1, key2 }) )(obj);
It allows me getting a new object without creating intermediate variables or objects.
For example -
var obj = {a:1, b:2, c:3};
var newObj = ( ({a, b}) => ({ a, b }) )(obj);
console.log(newObj);var obj = {a:1, b:2, c:3};
var newObj = ( ({a, b}) => ({ a, b }) )(obj);
console.log(newObj);var obj = {a:1, b:2, c:3};
var newObj = ( ({a, b}) => ({ a, b }) )(obj);
console.log(newObj);answered Nov 11 at 8:53
Arik
2,04311213
2,04311213
add a comment |
add a comment |
up vote
1
down vote
I'm not sure what you really want. But as far as I understood this will help you.
For example
I have the initial object
{ a: 10, b: 20, c: 100 }
Properties I want to extract are a, b
And I want to create new object c with property b, c
Here is how it is done. Using destructive assignment. Here the out put will be 20 as in the initial object.
({ a, b } = { a: 10, b: 20, c: 100 });
let c = {a: a, b: b};
console.log(c.b);MDN Document to destructive assignment. link
add a comment |
up vote
1
down vote
I'm not sure what you really want. But as far as I understood this will help you.
For example
I have the initial object
{ a: 10, b: 20, c: 100 }
Properties I want to extract are a, b
And I want to create new object c with property b, c
Here is how it is done. Using destructive assignment. Here the out put will be 20 as in the initial object.
({ a, b } = { a: 10, b: 20, c: 100 });
let c = {a: a, b: b};
console.log(c.b);MDN Document to destructive assignment. link
add a comment |
up vote
1
down vote
up vote
1
down vote
I'm not sure what you really want. But as far as I understood this will help you.
For example
I have the initial object
{ a: 10, b: 20, c: 100 }
Properties I want to extract are a, b
And I want to create new object c with property b, c
Here is how it is done. Using destructive assignment. Here the out put will be 20 as in the initial object.
({ a, b } = { a: 10, b: 20, c: 100 });
let c = {a: a, b: b};
console.log(c.b);MDN Document to destructive assignment. link
I'm not sure what you really want. But as far as I understood this will help you.
For example
I have the initial object
{ a: 10, b: 20, c: 100 }
Properties I want to extract are a, b
And I want to create new object c with property b, c
Here is how it is done. Using destructive assignment. Here the out put will be 20 as in the initial object.
({ a, b } = { a: 10, b: 20, c: 100 });
let c = {a: a, b: b};
console.log(c.b);MDN Document to destructive assignment. link
({ a, b } = { a: 10, b: 20, c: 100 });
let c = {a: a, b: b};
console.log(c.b);({ a, b } = { a: 10, b: 20, c: 100 });
let c = {a: a, b: b};
console.log(c.b);edited Jan 10 at 11:19
answered Jan 10 at 11:09
Shashith Darshana
7941020
7941020
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48185835%2fdestructing-partial-object-to-a-new-object-in-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Object spread/rest is not a part of the language yet, it will probably be included in ES 2018 though. There's not a easy syntax to do it.
– Axnyff
Jan 10 at 10:57
Cool, but I'm working with Babel
– Roni Gadot
Jan 10 at 11:00
@RoniGadot your
Const {a,...newObj} = originalworks fine. Do you want to achieve something else. Please explain– Rohit Agrawal
Jan 10 at 11:03
It works, but I want to cherry pick the fields I want and not omit the ones I don't
– Roni Gadot
Jan 10 at 11:10
@RoniGadot, added my answer according to your requirement. Please let me know in case of some problem.
– Rohit Agrawal
Jan 10 at 11:23