Is there a way to specify the order of the result of Object.values()
I get an item from an API in the following shape :
data : {
"56": { ... }, //item1
"57": { ... }, //item2
"58": { ... }, //item3
}
And I want an array of Object looking like this :
[
{ ... }, //item1 with key "56"
{ ... }, //item2 with key "57"
{ ... } //item3 with key "58"
]
This can of course be obtained with Object.values(data) . But since Javascript does not guarantee the order of an object's properties, I would like to find a way to use the properties' keys to sort the result and guarantee the order of the array of object I will get. Is this doable ?
Edit : as suggested in the comments, I could use Object.keys(), sort the result and then use it to build my array, but I am wondering if there is a more direct and elegant way to achieve this.
javascript
add a comment |
I get an item from an API in the following shape :
data : {
"56": { ... }, //item1
"57": { ... }, //item2
"58": { ... }, //item3
}
And I want an array of Object looking like this :
[
{ ... }, //item1 with key "56"
{ ... }, //item2 with key "57"
{ ... } //item3 with key "58"
]
This can of course be obtained with Object.values(data) . But since Javascript does not guarantee the order of an object's properties, I would like to find a way to use the properties' keys to sort the result and guarantee the order of the array of object I will get. Is this doable ?
Edit : as suggested in the comments, I could use Object.keys(), sort the result and then use it to build my array, but I am wondering if there is a more direct and elegant way to achieve this.
javascript
2
You could sort the data based on the values?
– JO3-W3B-D3V
Jan 2 at 15:43
1
UseObject.keys()to get the keys, sort that array however you want, and then.map()that onto a result array of values from the object.
– Pointy
Jan 2 at 15:45
you mean getObject.keys(), order the result, and then build the array from the result ? (Edit : as Pointy said while I was typing this comment) I could end up doing that but I wonder if there is a more elegant way of doing that. I'm kind of new to javascript and am always surprised by all the little tricks available.
– Tom
Jan 2 at 15:45
add a comment |
I get an item from an API in the following shape :
data : {
"56": { ... }, //item1
"57": { ... }, //item2
"58": { ... }, //item3
}
And I want an array of Object looking like this :
[
{ ... }, //item1 with key "56"
{ ... }, //item2 with key "57"
{ ... } //item3 with key "58"
]
This can of course be obtained with Object.values(data) . But since Javascript does not guarantee the order of an object's properties, I would like to find a way to use the properties' keys to sort the result and guarantee the order of the array of object I will get. Is this doable ?
Edit : as suggested in the comments, I could use Object.keys(), sort the result and then use it to build my array, but I am wondering if there is a more direct and elegant way to achieve this.
javascript
I get an item from an API in the following shape :
data : {
"56": { ... }, //item1
"57": { ... }, //item2
"58": { ... }, //item3
}
And I want an array of Object looking like this :
[
{ ... }, //item1 with key "56"
{ ... }, //item2 with key "57"
{ ... } //item3 with key "58"
]
This can of course be obtained with Object.values(data) . But since Javascript does not guarantee the order of an object's properties, I would like to find a way to use the properties' keys to sort the result and guarantee the order of the array of object I will get. Is this doable ?
Edit : as suggested in the comments, I could use Object.keys(), sort the result and then use it to build my array, but I am wondering if there is a more direct and elegant way to achieve this.
javascript
javascript
edited Jan 2 at 15:47
Tom
asked Jan 2 at 15:42
TomTom
500114
500114
2
You could sort the data based on the values?
– JO3-W3B-D3V
Jan 2 at 15:43
1
UseObject.keys()to get the keys, sort that array however you want, and then.map()that onto a result array of values from the object.
– Pointy
Jan 2 at 15:45
you mean getObject.keys(), order the result, and then build the array from the result ? (Edit : as Pointy said while I was typing this comment) I could end up doing that but I wonder if there is a more elegant way of doing that. I'm kind of new to javascript and am always surprised by all the little tricks available.
– Tom
Jan 2 at 15:45
add a comment |
2
You could sort the data based on the values?
– JO3-W3B-D3V
Jan 2 at 15:43
1
UseObject.keys()to get the keys, sort that array however you want, and then.map()that onto a result array of values from the object.
– Pointy
Jan 2 at 15:45
you mean getObject.keys(), order the result, and then build the array from the result ? (Edit : as Pointy said while I was typing this comment) I could end up doing that but I wonder if there is a more elegant way of doing that. I'm kind of new to javascript and am always surprised by all the little tricks available.
– Tom
Jan 2 at 15:45
2
2
You could sort the data based on the values?
– JO3-W3B-D3V
Jan 2 at 15:43
You could sort the data based on the values?
– JO3-W3B-D3V
Jan 2 at 15:43
1
1
Use
Object.keys() to get the keys, sort that array however you want, and then .map() that onto a result array of values from the object.– Pointy
Jan 2 at 15:45
Use
Object.keys() to get the keys, sort that array however you want, and then .map() that onto a result array of values from the object.– Pointy
Jan 2 at 15:45
you mean get
Object.keys(), order the result, and then build the array from the result ? (Edit : as Pointy said while I was typing this comment) I could end up doing that but I wonder if there is a more elegant way of doing that. I'm kind of new to javascript and am always surprised by all the little tricks available.– Tom
Jan 2 at 15:45
you mean get
Object.keys(), order the result, and then build the array from the result ? (Edit : as Pointy said while I was typing this comment) I could end up doing that but I wonder if there is a more elegant way of doing that. I'm kind of new to javascript and am always surprised by all the little tricks available.– Tom
Jan 2 at 15:45
add a comment |
1 Answer
1
active
oldest
votes
You could get the entries, sort them and map only the values.
data = Object
.entries(object.data)
.sort(([a], [b]) => a - b)
.map(([, v]) => v)
I personally highly appreciate this answer! Nice!
– JO3-W3B-D3V
Jan 2 at 15:47
Very close to what was proposed in the comment, but still very elegant, I like it a lot. Validating in 3 minutes when allowed, thank you !
– Tom
Jan 2 at 15:53
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%2f54009160%2fis-there-a-way-to-specify-the-order-of-the-result-of-object-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could get the entries, sort them and map only the values.
data = Object
.entries(object.data)
.sort(([a], [b]) => a - b)
.map(([, v]) => v)
I personally highly appreciate this answer! Nice!
– JO3-W3B-D3V
Jan 2 at 15:47
Very close to what was proposed in the comment, but still very elegant, I like it a lot. Validating in 3 minutes when allowed, thank you !
– Tom
Jan 2 at 15:53
add a comment |
You could get the entries, sort them and map only the values.
data = Object
.entries(object.data)
.sort(([a], [b]) => a - b)
.map(([, v]) => v)
I personally highly appreciate this answer! Nice!
– JO3-W3B-D3V
Jan 2 at 15:47
Very close to what was proposed in the comment, but still very elegant, I like it a lot. Validating in 3 minutes when allowed, thank you !
– Tom
Jan 2 at 15:53
add a comment |
You could get the entries, sort them and map only the values.
data = Object
.entries(object.data)
.sort(([a], [b]) => a - b)
.map(([, v]) => v)
You could get the entries, sort them and map only the values.
data = Object
.entries(object.data)
.sort(([a], [b]) => a - b)
.map(([, v]) => v)
answered Jan 2 at 15:47
Nina ScholzNina Scholz
191k15103175
191k15103175
I personally highly appreciate this answer! Nice!
– JO3-W3B-D3V
Jan 2 at 15:47
Very close to what was proposed in the comment, but still very elegant, I like it a lot. Validating in 3 minutes when allowed, thank you !
– Tom
Jan 2 at 15:53
add a comment |
I personally highly appreciate this answer! Nice!
– JO3-W3B-D3V
Jan 2 at 15:47
Very close to what was proposed in the comment, but still very elegant, I like it a lot. Validating in 3 minutes when allowed, thank you !
– Tom
Jan 2 at 15:53
I personally highly appreciate this answer! Nice!
– JO3-W3B-D3V
Jan 2 at 15:47
I personally highly appreciate this answer! Nice!
– JO3-W3B-D3V
Jan 2 at 15:47
Very close to what was proposed in the comment, but still very elegant, I like it a lot. Validating in 3 minutes when allowed, thank you !
– Tom
Jan 2 at 15:53
Very close to what was proposed in the comment, but still very elegant, I like it a lot. Validating in 3 minutes when allowed, thank you !
– Tom
Jan 2 at 15:53
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%2f54009160%2fis-there-a-way-to-specify-the-order-of-the-result-of-object-values%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
2
You could sort the data based on the values?
– JO3-W3B-D3V
Jan 2 at 15:43
1
Use
Object.keys()to get the keys, sort that array however you want, and then.map()that onto a result array of values from the object.– Pointy
Jan 2 at 15:45
you mean get
Object.keys(), order the result, and then build the array from the result ? (Edit : as Pointy said while I was typing this comment) I could end up doing that but I wonder if there is a more elegant way of doing that. I'm kind of new to javascript and am always surprised by all the little tricks available.– Tom
Jan 2 at 15:45