Destructuring for get the first property of an object?
For arrays, we can define the properties depending on it's indexes like:
const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)
I'm just wondering if there's a possible solution to do it's reverse with objects like:
const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"
javascript
|
show 3 more comments
For arrays, we can define the properties depending on it's indexes like:
const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)
I'm just wondering if there's a possible solution to do it's reverse with objects like:
const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"
javascript
4
Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.
– jonrsharpe
Nov 16 '18 at 11:22
1
Keys in object don't have an order You can use Map or Array only
– HMR
Nov 16 '18 at 11:23
@jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.
– Gergő Horváth
Nov 16 '18 at 11:27
2
No it doesn't, I get aSyntaxError
(e.g.missing variable name
orunexpected string
, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.
– jonrsharpe
Nov 16 '18 at 11:29
1
please add the wanted result as well.
– Nina Scholz
Nov 16 '18 at 11:29
|
show 3 more comments
For arrays, we can define the properties depending on it's indexes like:
const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)
I'm just wondering if there's a possible solution to do it's reverse with objects like:
const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"
javascript
For arrays, we can define the properties depending on it's indexes like:
const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)
I'm just wondering if there's a possible solution to do it's reverse with objects like:
const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"
const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)
const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)
const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"
const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"
javascript
javascript
edited Nov 16 '18 at 11:32
Gergő Horváth
asked Nov 16 '18 at 11:20
Gergő HorváthGergő Horváth
566115
566115
4
Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.
– jonrsharpe
Nov 16 '18 at 11:22
1
Keys in object don't have an order You can use Map or Array only
– HMR
Nov 16 '18 at 11:23
@jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.
– Gergő Horváth
Nov 16 '18 at 11:27
2
No it doesn't, I get aSyntaxError
(e.g.missing variable name
orunexpected string
, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.
– jonrsharpe
Nov 16 '18 at 11:29
1
please add the wanted result as well.
– Nina Scholz
Nov 16 '18 at 11:29
|
show 3 more comments
4
Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.
– jonrsharpe
Nov 16 '18 at 11:22
1
Keys in object don't have an order You can use Map or Array only
– HMR
Nov 16 '18 at 11:23
@jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.
– Gergő Horváth
Nov 16 '18 at 11:27
2
No it doesn't, I get aSyntaxError
(e.g.missing variable name
orunexpected string
, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.
– jonrsharpe
Nov 16 '18 at 11:29
1
please add the wanted result as well.
– Nina Scholz
Nov 16 '18 at 11:29
4
4
Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.
– jonrsharpe
Nov 16 '18 at 11:22
Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.
– jonrsharpe
Nov 16 '18 at 11:22
1
1
Keys in object don't have an order You can use Map or Array only
– HMR
Nov 16 '18 at 11:23
Keys in object don't have an order You can use Map or Array only
– HMR
Nov 16 '18 at 11:23
@jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.
– Gergő Horváth
Nov 16 '18 at 11:27
@jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.
– Gergő Horváth
Nov 16 '18 at 11:27
2
2
No it doesn't, I get a
SyntaxError
(e.g. missing variable name
or unexpected string
, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.– jonrsharpe
Nov 16 '18 at 11:29
No it doesn't, I get a
SyntaxError
(e.g. missing variable name
or unexpected string
, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.– jonrsharpe
Nov 16 '18 at 11:29
1
1
please add the wanted result as well.
– Nina Scholz
Nov 16 '18 at 11:29
please add the wanted result as well.
– Nina Scholz
Nov 16 '18 at 11:29
|
show 3 more comments
4 Answers
4
active
oldest
votes
You could take the values and assign this to an array for destructuring.
The order is actually dtermined by the insertation order or if a key is like a valid index of an array, it is sorted numerically to top.
const
object = { first: "a", second: "b", third: "c" },
[first, second, third] = Object.values(object);
console.log(first, second, third);
For extracting a an arbitrary position, you vould take an object with an index an object property assignment pattern [YDKJS: ES6 & Beyond] for a new valid variable.
const
object = { first: "a", second: "b", third: "c" },
{ 2: foo } = Object.values(object);
console.log(foo);
add a comment |
You do it like this for objects:
const obj = {foo: 123, bar: 'str'}
const {foo, bar} = obj
add a comment |
It isn't.
Objects are not designed to be ordered, so there isn't a first property per se.
You could convert an object into an array of its values first …
const obj = {
first: "a",
second: "b",
third: "c"
}
const array = Object.values(obj);
const [foo, bar, baz] = array;
console.log({
foo,
bar,
baz
});
… but it is unlikely to be useful and it certainly wouldn't be intuitive code that is easy to maintain.
add a comment |
Try this:
const obj = {first: "a", second: "b", third: "c"}
const indexes = [0, 1, 2]
indexes.map( (val) => { return Object.values(obj)[val] } ) //["a", "b", "c"]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53336844%2fdestructuring-for-get-the-first-property-of-an-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could take the values and assign this to an array for destructuring.
The order is actually dtermined by the insertation order or if a key is like a valid index of an array, it is sorted numerically to top.
const
object = { first: "a", second: "b", third: "c" },
[first, second, third] = Object.values(object);
console.log(first, second, third);
For extracting a an arbitrary position, you vould take an object with an index an object property assignment pattern [YDKJS: ES6 & Beyond] for a new valid variable.
const
object = { first: "a", second: "b", third: "c" },
{ 2: foo } = Object.values(object);
console.log(foo);
add a comment |
You could take the values and assign this to an array for destructuring.
The order is actually dtermined by the insertation order or if a key is like a valid index of an array, it is sorted numerically to top.
const
object = { first: "a", second: "b", third: "c" },
[first, second, third] = Object.values(object);
console.log(first, second, third);
For extracting a an arbitrary position, you vould take an object with an index an object property assignment pattern [YDKJS: ES6 & Beyond] for a new valid variable.
const
object = { first: "a", second: "b", third: "c" },
{ 2: foo } = Object.values(object);
console.log(foo);
add a comment |
You could take the values and assign this to an array for destructuring.
The order is actually dtermined by the insertation order or if a key is like a valid index of an array, it is sorted numerically to top.
const
object = { first: "a", second: "b", third: "c" },
[first, second, third] = Object.values(object);
console.log(first, second, third);
For extracting a an arbitrary position, you vould take an object with an index an object property assignment pattern [YDKJS: ES6 & Beyond] for a new valid variable.
const
object = { first: "a", second: "b", third: "c" },
{ 2: foo } = Object.values(object);
console.log(foo);
You could take the values and assign this to an array for destructuring.
The order is actually dtermined by the insertation order or if a key is like a valid index of an array, it is sorted numerically to top.
const
object = { first: "a", second: "b", third: "c" },
[first, second, third] = Object.values(object);
console.log(first, second, third);
For extracting a an arbitrary position, you vould take an object with an index an object property assignment pattern [YDKJS: ES6 & Beyond] for a new valid variable.
const
object = { first: "a", second: "b", third: "c" },
{ 2: foo } = Object.values(object);
console.log(foo);
const
object = { first: "a", second: "b", third: "c" },
[first, second, third] = Object.values(object);
console.log(first, second, third);
const
object = { first: "a", second: "b", third: "c" },
[first, second, third] = Object.values(object);
console.log(first, second, third);
const
object = { first: "a", second: "b", third: "c" },
{ 2: foo } = Object.values(object);
console.log(foo);
const
object = { first: "a", second: "b", third: "c" },
{ 2: foo } = Object.values(object);
console.log(foo);
answered Nov 16 '18 at 11:39
Nina ScholzNina Scholz
195k15107179
195k15107179
add a comment |
add a comment |
You do it like this for objects:
const obj = {foo: 123, bar: 'str'}
const {foo, bar} = obj
add a comment |
You do it like this for objects:
const obj = {foo: 123, bar: 'str'}
const {foo, bar} = obj
add a comment |
You do it like this for objects:
const obj = {foo: 123, bar: 'str'}
const {foo, bar} = obj
You do it like this for objects:
const obj = {foo: 123, bar: 'str'}
const {foo, bar} = obj
answered Nov 16 '18 at 11:34
Nurbol AlpysbayevNurbol Alpysbayev
4,8241533
4,8241533
add a comment |
add a comment |
It isn't.
Objects are not designed to be ordered, so there isn't a first property per se.
You could convert an object into an array of its values first …
const obj = {
first: "a",
second: "b",
third: "c"
}
const array = Object.values(obj);
const [foo, bar, baz] = array;
console.log({
foo,
bar,
baz
});
… but it is unlikely to be useful and it certainly wouldn't be intuitive code that is easy to maintain.
add a comment |
It isn't.
Objects are not designed to be ordered, so there isn't a first property per se.
You could convert an object into an array of its values first …
const obj = {
first: "a",
second: "b",
third: "c"
}
const array = Object.values(obj);
const [foo, bar, baz] = array;
console.log({
foo,
bar,
baz
});
… but it is unlikely to be useful and it certainly wouldn't be intuitive code that is easy to maintain.
add a comment |
It isn't.
Objects are not designed to be ordered, so there isn't a first property per se.
You could convert an object into an array of its values first …
const obj = {
first: "a",
second: "b",
third: "c"
}
const array = Object.values(obj);
const [foo, bar, baz] = array;
console.log({
foo,
bar,
baz
});
… but it is unlikely to be useful and it certainly wouldn't be intuitive code that is easy to maintain.
It isn't.
Objects are not designed to be ordered, so there isn't a first property per se.
You could convert an object into an array of its values first …
const obj = {
first: "a",
second: "b",
third: "c"
}
const array = Object.values(obj);
const [foo, bar, baz] = array;
console.log({
foo,
bar,
baz
});
… but it is unlikely to be useful and it certainly wouldn't be intuitive code that is easy to maintain.
const obj = {
first: "a",
second: "b",
third: "c"
}
const array = Object.values(obj);
const [foo, bar, baz] = array;
console.log({
foo,
bar,
baz
});
const obj = {
first: "a",
second: "b",
third: "c"
}
const array = Object.values(obj);
const [foo, bar, baz] = array;
console.log({
foo,
bar,
baz
});
edited Nov 16 '18 at 11:38
answered Nov 16 '18 at 11:22
QuentinQuentin
657k728931056
657k728931056
add a comment |
add a comment |
Try this:
const obj = {first: "a", second: "b", third: "c"}
const indexes = [0, 1, 2]
indexes.map( (val) => { return Object.values(obj)[val] } ) //["a", "b", "c"]
add a comment |
Try this:
const obj = {first: "a", second: "b", third: "c"}
const indexes = [0, 1, 2]
indexes.map( (val) => { return Object.values(obj)[val] } ) //["a", "b", "c"]
add a comment |
Try this:
const obj = {first: "a", second: "b", third: "c"}
const indexes = [0, 1, 2]
indexes.map( (val) => { return Object.values(obj)[val] } ) //["a", "b", "c"]
Try this:
const obj = {first: "a", second: "b", third: "c"}
const indexes = [0, 1, 2]
indexes.map( (val) => { return Object.values(obj)[val] } ) //["a", "b", "c"]
answered Nov 16 '18 at 11:51
javimovijavimovi
318110
318110
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.
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%2f53336844%2fdestructuring-for-get-the-first-property-of-an-object%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
4
Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.
– jonrsharpe
Nov 16 '18 at 11:22
1
Keys in object don't have an order You can use Map or Array only
– HMR
Nov 16 '18 at 11:23
@jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.
– Gergő Horváth
Nov 16 '18 at 11:27
2
No it doesn't, I get a
SyntaxError
(e.g.missing variable name
orunexpected string
, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.– jonrsharpe
Nov 16 '18 at 11:29
1
please add the wanted result as well.
– Nina Scholz
Nov 16 '18 at 11:29