Is there a way to specify the order of the result of Object.values()












1















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.










share|improve this question




















  • 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


















1















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.










share|improve this question




















  • 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
















1












1








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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





    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
















  • 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










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














1 Answer
1






active

oldest

votes


















5














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)





share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









5














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)





share|improve this answer
























  • 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
















5














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)





share|improve this answer
























  • 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














5












5








5







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)





share|improve this answer













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)






share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'