Order nested objects by parameter JS
Is there a way to sort nested objects by one of their parameters?
For example, if I have a data structure like this:
var someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
and I don't know how many parts I'll have in advance or what their names will be. Is there a way I can sort the parts by their time and get the most recent and oldest parts?
javascript object
|
show 2 more comments
Is there a way to sort nested objects by one of their parameters?
For example, if I have a data structure like this:
var someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
and I don't know how many parts I'll have in advance or what their names will be. Is there a way I can sort the parts by their time and get the most recent and oldest parts?
javascript object
1
Could you add what the output should be?
– dwjohnston
Nov 26 '18 at 3:37
1
You should not assume that an object has some kind of ordering. So what do you mean by the "first" and "last" part? Are they determined by the number in their key?
– slider
Nov 26 '18 at 3:38
2
IMK Property order is not mantianed in Javascript objects so first/last can differ
– xyz
Nov 26 '18 at 3:38
1
Objects do not keep insertion order state; Maps do, however. You will have to elaborate on expected output and how this may be sorted given an object.
– Rafael
Nov 26 '18 at 3:41
Other things aside, you will be doing your future self a favor if you are consistent about the values your object. Right now some are objects and one is an array. It will make this an annoying data structure.
– Mark Meyer
Nov 26 '18 at 3:43
|
show 2 more comments
Is there a way to sort nested objects by one of their parameters?
For example, if I have a data structure like this:
var someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
and I don't know how many parts I'll have in advance or what their names will be. Is there a way I can sort the parts by their time and get the most recent and oldest parts?
javascript object
Is there a way to sort nested objects by one of their parameters?
For example, if I have a data structure like this:
var someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
and I don't know how many parts I'll have in advance or what their names will be. Is there a way I can sort the parts by their time and get the most recent and oldest parts?
javascript object
javascript object
edited Nov 26 '18 at 3:44
TransmissionsDev
asked Nov 26 '18 at 3:34
TransmissionsDevTransmissionsDev
338
338
1
Could you add what the output should be?
– dwjohnston
Nov 26 '18 at 3:37
1
You should not assume that an object has some kind of ordering. So what do you mean by the "first" and "last" part? Are they determined by the number in their key?
– slider
Nov 26 '18 at 3:38
2
IMK Property order is not mantianed in Javascript objects so first/last can differ
– xyz
Nov 26 '18 at 3:38
1
Objects do not keep insertion order state; Maps do, however. You will have to elaborate on expected output and how this may be sorted given an object.
– Rafael
Nov 26 '18 at 3:41
Other things aside, you will be doing your future self a favor if you are consistent about the values your object. Right now some are objects and one is an array. It will make this an annoying data structure.
– Mark Meyer
Nov 26 '18 at 3:43
|
show 2 more comments
1
Could you add what the output should be?
– dwjohnston
Nov 26 '18 at 3:37
1
You should not assume that an object has some kind of ordering. So what do you mean by the "first" and "last" part? Are they determined by the number in their key?
– slider
Nov 26 '18 at 3:38
2
IMK Property order is not mantianed in Javascript objects so first/last can differ
– xyz
Nov 26 '18 at 3:38
1
Objects do not keep insertion order state; Maps do, however. You will have to elaborate on expected output and how this may be sorted given an object.
– Rafael
Nov 26 '18 at 3:41
Other things aside, you will be doing your future self a favor if you are consistent about the values your object. Right now some are objects and one is an array. It will make this an annoying data structure.
– Mark Meyer
Nov 26 '18 at 3:43
1
1
Could you add what the output should be?
– dwjohnston
Nov 26 '18 at 3:37
Could you add what the output should be?
– dwjohnston
Nov 26 '18 at 3:37
1
1
You should not assume that an object has some kind of ordering. So what do you mean by the "first" and "last" part? Are they determined by the number in their key?
– slider
Nov 26 '18 at 3:38
You should not assume that an object has some kind of ordering. So what do you mean by the "first" and "last" part? Are they determined by the number in their key?
– slider
Nov 26 '18 at 3:38
2
2
IMK Property order is not mantianed in Javascript objects so first/last can differ
– xyz
Nov 26 '18 at 3:38
IMK Property order is not mantianed in Javascript objects so first/last can differ
– xyz
Nov 26 '18 at 3:38
1
1
Objects do not keep insertion order state; Maps do, however. You will have to elaborate on expected output and how this may be sorted given an object.
– Rafael
Nov 26 '18 at 3:41
Objects do not keep insertion order state; Maps do, however. You will have to elaborate on expected output and how this may be sorted given an object.
– Rafael
Nov 26 '18 at 3:41
Other things aside, you will be doing your future self a favor if you are consistent about the values your object. Right now some are objects and one is an array. It will make this an annoying data structure.
– Mark Meyer
Nov 26 '18 at 3:43
Other things aside, you will be doing your future self a favor if you are consistent about the values your object. Right now some are objects and one is an array. It will make this an annoying data structure.
– Mark Meyer
Nov 26 '18 at 3:43
|
show 2 more comments
3 Answers
3
active
oldest
votes
You can use Object.entries
to get the set of key/value pairs as a list. Then you can sort that list and arrange the data however you like:
var someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
let arr = Object.entries(someObject).sort((a, b) => a.time - b.time)
console.log(arr)
// from here you can manage the data any way you want.
// for example, an array of simple objects:
let merged = arr.map(([key, value]) => ({id: key, ...value}) )
console.log(merged)
Is the benefit of using Object.entries that I get the full object, not just the key name sorted (this is in reference to Amadan's solution)?
– TransmissionsDev
Nov 26 '18 at 4:02
1
@TransmissionsDev yes Object.entries is a little more convenient as it gets you a list[key, value]
pairs. You can also useObject.keys
and then lookup the value. One import difference is thatObject.keys
is supported by IE andentries()
isn't.
– Mark Meyer
Nov 26 '18 at 4:09
add a comment |
You cannot sort an object. You can sort a list of object's keys, you can sort object's values, or the list of pairs of a key and a corresponding value ("entries"). Here's the first approach:
Object.keys(someObject).sort((a, b) => a.time - b.time)
// => ["part1328", "part38321", "part38338"]
You can then use these sorted keys to access the values in the original object in the desired order.
Note also that objects can't have repeating keys; they just overwrite each other. Thus, the fourth value is gone even before you assigned it to someObject
.
Would I then just use bracket notation to retrieve the actual part?
– TransmissionsDev
Nov 26 '18 at 3:58
1
Yeah, sosomeObject[sortedKeys[0]]
would be the earliest record.
– Amadan
Nov 26 '18 at 3:59
add a comment |
You can maintain the sorted list by creating an array
sorted by time
Code:
const someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
const result = Object.keys(someObject)
.sort((a, b) => someObject[a].time - someObject[b].time)
.map(k => ({ [k]: someObject[k] }));
console.log(result);
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%2f53474476%2forder-nested-objects-by-parameter-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Object.entries
to get the set of key/value pairs as a list. Then you can sort that list and arrange the data however you like:
var someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
let arr = Object.entries(someObject).sort((a, b) => a.time - b.time)
console.log(arr)
// from here you can manage the data any way you want.
// for example, an array of simple objects:
let merged = arr.map(([key, value]) => ({id: key, ...value}) )
console.log(merged)
Is the benefit of using Object.entries that I get the full object, not just the key name sorted (this is in reference to Amadan's solution)?
– TransmissionsDev
Nov 26 '18 at 4:02
1
@TransmissionsDev yes Object.entries is a little more convenient as it gets you a list[key, value]
pairs. You can also useObject.keys
and then lookup the value. One import difference is thatObject.keys
is supported by IE andentries()
isn't.
– Mark Meyer
Nov 26 '18 at 4:09
add a comment |
You can use Object.entries
to get the set of key/value pairs as a list. Then you can sort that list and arrange the data however you like:
var someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
let arr = Object.entries(someObject).sort((a, b) => a.time - b.time)
console.log(arr)
// from here you can manage the data any way you want.
// for example, an array of simple objects:
let merged = arr.map(([key, value]) => ({id: key, ...value}) )
console.log(merged)
Is the benefit of using Object.entries that I get the full object, not just the key name sorted (this is in reference to Amadan's solution)?
– TransmissionsDev
Nov 26 '18 at 4:02
1
@TransmissionsDev yes Object.entries is a little more convenient as it gets you a list[key, value]
pairs. You can also useObject.keys
and then lookup the value. One import difference is thatObject.keys
is supported by IE andentries()
isn't.
– Mark Meyer
Nov 26 '18 at 4:09
add a comment |
You can use Object.entries
to get the set of key/value pairs as a list. Then you can sort that list and arrange the data however you like:
var someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
let arr = Object.entries(someObject).sort((a, b) => a.time - b.time)
console.log(arr)
// from here you can manage the data any way you want.
// for example, an array of simple objects:
let merged = arr.map(([key, value]) => ({id: key, ...value}) )
console.log(merged)
You can use Object.entries
to get the set of key/value pairs as a list. Then you can sort that list and arrange the data however you like:
var someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
let arr = Object.entries(someObject).sort((a, b) => a.time - b.time)
console.log(arr)
// from here you can manage the data any way you want.
// for example, an array of simple objects:
let merged = arr.map(([key, value]) => ({id: key, ...value}) )
console.log(merged)
var someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
let arr = Object.entries(someObject).sort((a, b) => a.time - b.time)
console.log(arr)
// from here you can manage the data any way you want.
// for example, an array of simple objects:
let merged = arr.map(([key, value]) => ({id: key, ...value}) )
console.log(merged)
var someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
let arr = Object.entries(someObject).sort((a, b) => a.time - b.time)
console.log(arr)
// from here you can manage the data any way you want.
// for example, an array of simple objects:
let merged = arr.map(([key, value]) => ({id: key, ...value}) )
console.log(merged)
answered Nov 26 '18 at 3:49
Mark MeyerMark Meyer
38.2k33159
38.2k33159
Is the benefit of using Object.entries that I get the full object, not just the key name sorted (this is in reference to Amadan's solution)?
– TransmissionsDev
Nov 26 '18 at 4:02
1
@TransmissionsDev yes Object.entries is a little more convenient as it gets you a list[key, value]
pairs. You can also useObject.keys
and then lookup the value. One import difference is thatObject.keys
is supported by IE andentries()
isn't.
– Mark Meyer
Nov 26 '18 at 4:09
add a comment |
Is the benefit of using Object.entries that I get the full object, not just the key name sorted (this is in reference to Amadan's solution)?
– TransmissionsDev
Nov 26 '18 at 4:02
1
@TransmissionsDev yes Object.entries is a little more convenient as it gets you a list[key, value]
pairs. You can also useObject.keys
and then lookup the value. One import difference is thatObject.keys
is supported by IE andentries()
isn't.
– Mark Meyer
Nov 26 '18 at 4:09
Is the benefit of using Object.entries that I get the full object, not just the key name sorted (this is in reference to Amadan's solution)?
– TransmissionsDev
Nov 26 '18 at 4:02
Is the benefit of using Object.entries that I get the full object, not just the key name sorted (this is in reference to Amadan's solution)?
– TransmissionsDev
Nov 26 '18 at 4:02
1
1
@TransmissionsDev yes Object.entries is a little more convenient as it gets you a list
[key, value]
pairs. You can also use Object.keys
and then lookup the value. One import difference is that Object.keys
is supported by IE and entries()
isn't.– Mark Meyer
Nov 26 '18 at 4:09
@TransmissionsDev yes Object.entries is a little more convenient as it gets you a list
[key, value]
pairs. You can also use Object.keys
and then lookup the value. One import difference is that Object.keys
is supported by IE and entries()
isn't.– Mark Meyer
Nov 26 '18 at 4:09
add a comment |
You cannot sort an object. You can sort a list of object's keys, you can sort object's values, or the list of pairs of a key and a corresponding value ("entries"). Here's the first approach:
Object.keys(someObject).sort((a, b) => a.time - b.time)
// => ["part1328", "part38321", "part38338"]
You can then use these sorted keys to access the values in the original object in the desired order.
Note also that objects can't have repeating keys; they just overwrite each other. Thus, the fourth value is gone even before you assigned it to someObject
.
Would I then just use bracket notation to retrieve the actual part?
– TransmissionsDev
Nov 26 '18 at 3:58
1
Yeah, sosomeObject[sortedKeys[0]]
would be the earliest record.
– Amadan
Nov 26 '18 at 3:59
add a comment |
You cannot sort an object. You can sort a list of object's keys, you can sort object's values, or the list of pairs of a key and a corresponding value ("entries"). Here's the first approach:
Object.keys(someObject).sort((a, b) => a.time - b.time)
// => ["part1328", "part38321", "part38338"]
You can then use these sorted keys to access the values in the original object in the desired order.
Note also that objects can't have repeating keys; they just overwrite each other. Thus, the fourth value is gone even before you assigned it to someObject
.
Would I then just use bracket notation to retrieve the actual part?
– TransmissionsDev
Nov 26 '18 at 3:58
1
Yeah, sosomeObject[sortedKeys[0]]
would be the earliest record.
– Amadan
Nov 26 '18 at 3:59
add a comment |
You cannot sort an object. You can sort a list of object's keys, you can sort object's values, or the list of pairs of a key and a corresponding value ("entries"). Here's the first approach:
Object.keys(someObject).sort((a, b) => a.time - b.time)
// => ["part1328", "part38321", "part38338"]
You can then use these sorted keys to access the values in the original object in the desired order.
Note also that objects can't have repeating keys; they just overwrite each other. Thus, the fourth value is gone even before you assigned it to someObject
.
You cannot sort an object. You can sort a list of object's keys, you can sort object's values, or the list of pairs of a key and a corresponding value ("entries"). Here's the first approach:
Object.keys(someObject).sort((a, b) => a.time - b.time)
// => ["part1328", "part38321", "part38338"]
You can then use these sorted keys to access the values in the original object in the desired order.
Note also that objects can't have repeating keys; they just overwrite each other. Thus, the fourth value is gone even before you assigned it to someObject
.
answered Nov 26 '18 at 3:50
AmadanAmadan
130k13142193
130k13142193
Would I then just use bracket notation to retrieve the actual part?
– TransmissionsDev
Nov 26 '18 at 3:58
1
Yeah, sosomeObject[sortedKeys[0]]
would be the earliest record.
– Amadan
Nov 26 '18 at 3:59
add a comment |
Would I then just use bracket notation to retrieve the actual part?
– TransmissionsDev
Nov 26 '18 at 3:58
1
Yeah, sosomeObject[sortedKeys[0]]
would be the earliest record.
– Amadan
Nov 26 '18 at 3:59
Would I then just use bracket notation to retrieve the actual part?
– TransmissionsDev
Nov 26 '18 at 3:58
Would I then just use bracket notation to retrieve the actual part?
– TransmissionsDev
Nov 26 '18 at 3:58
1
1
Yeah, so
someObject[sortedKeys[0]]
would be the earliest record.– Amadan
Nov 26 '18 at 3:59
Yeah, so
someObject[sortedKeys[0]]
would be the earliest record.– Amadan
Nov 26 '18 at 3:59
add a comment |
You can maintain the sorted list by creating an array
sorted by time
Code:
const someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
const result = Object.keys(someObject)
.sort((a, b) => someObject[a].time - someObject[b].time)
.map(k => ({ [k]: someObject[k] }));
console.log(result);
add a comment |
You can maintain the sorted list by creating an array
sorted by time
Code:
const someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
const result = Object.keys(someObject)
.sort((a, b) => someObject[a].time - someObject[b].time)
.map(k => ({ [k]: someObject[k] }));
console.log(result);
add a comment |
You can maintain the sorted list by creating an array
sorted by time
Code:
const someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
const result = Object.keys(someObject)
.sort((a, b) => someObject[a].time - someObject[b].time)
.map(k => ({ [k]: someObject[k] }));
console.log(result);
You can maintain the sorted list by creating an array
sorted by time
Code:
const someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
const result = Object.keys(someObject)
.sort((a, b) => someObject[a].time - someObject[b].time)
.map(k => ({ [k]: someObject[k] }));
console.log(result);
const someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
const result = Object.keys(someObject)
.sort((a, b) => someObject[a].time - someObject[b].time)
.map(k => ({ [k]: someObject[k] }));
console.log(result);
const someObject = {
'part1328': {
'time': 1543203609575,
},
'part38321': {
'time': 1543203738716,
},
'part1328': {
'time': 1543203746046,
},
'part38338': {
'time': 1543203752264,
}
};
const result = Object.keys(someObject)
.sort((a, b) => someObject[a].time - someObject[b].time)
.map(k => ({ [k]: someObject[k] }));
console.log(result);
answered Nov 26 '18 at 3:51
Yosvel QuinteroYosvel Quintero
11.1k42430
11.1k42430
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%2f53474476%2forder-nested-objects-by-parameter-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
1
Could you add what the output should be?
– dwjohnston
Nov 26 '18 at 3:37
1
You should not assume that an object has some kind of ordering. So what do you mean by the "first" and "last" part? Are they determined by the number in their key?
– slider
Nov 26 '18 at 3:38
2
IMK Property order is not mantianed in Javascript objects so first/last can differ
– xyz
Nov 26 '18 at 3:38
1
Objects do not keep insertion order state; Maps do, however. You will have to elaborate on expected output and how this may be sorted given an object.
– Rafael
Nov 26 '18 at 3:41
Other things aside, you will be doing your future self a favor if you are consistent about the values your object. Right now some are objects and one is an array. It will make this an annoying data structure.
– Mark Meyer
Nov 26 '18 at 3:43