How to reverse nested object in Javascript [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
How to reverse an object in javascript? [duplicate]
4 answers
I have an object like such:
{ '2018':
{ '12':
{ '25': {},
'26': {},
'27': {},
'28': {},
'29': {},
'30': {},
'31': {} } },
'2019': { '1': { '1': {} } } }
However for frontend purposes, I would like to reverse these values so it shows the most recent dates first. I know that Javascript can't guarantee the order of keys so this isn't even an ideal solution. What is the best way to represent this data so I can iterate over it on the frontend and display the data for each day properly? Thanks
javascript arrays node.js object
marked as duplicate by Robert Harvey♦ Jan 3 at 5:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to reverse an object in javascript? [duplicate]
4 answers
I have an object like such:
{ '2018':
{ '12':
{ '25': {},
'26': {},
'27': {},
'28': {},
'29': {},
'30': {},
'31': {} } },
'2019': { '1': { '1': {} } } }
However for frontend purposes, I would like to reverse these values so it shows the most recent dates first. I know that Javascript can't guarantee the order of keys so this isn't even an ideal solution. What is the best way to represent this data so I can iterate over it on the frontend and display the data for each day properly? Thanks
javascript arrays node.js object
marked as duplicate by Robert Harvey♦ Jan 3 at 5:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
please share expected out structure.
– Dipak
Jan 3 at 5:34
@Robert Harvey, that question doesn't deal with nested data, besides Javascript doesn't guarantee order with so it's not even a true answer there
– Antoine
Jan 3 at 5:45
@RoberHarvey you did link this question to another closed question which is linked with the order of keys in an object. Maybe his goal was to have anyway a reverted strucutre (as an array) composed by objects in the opposite order
– quirimmo
Jan 3 at 5:48
@quirimmo I've actually designed a solution I'll post it once I confirm it works
– Antoine
Jan 3 at 5:50
add a comment |
This question already has an answer here:
How to reverse an object in javascript? [duplicate]
4 answers
I have an object like such:
{ '2018':
{ '12':
{ '25': {},
'26': {},
'27': {},
'28': {},
'29': {},
'30': {},
'31': {} } },
'2019': { '1': { '1': {} } } }
However for frontend purposes, I would like to reverse these values so it shows the most recent dates first. I know that Javascript can't guarantee the order of keys so this isn't even an ideal solution. What is the best way to represent this data so I can iterate over it on the frontend and display the data for each day properly? Thanks
javascript arrays node.js object
This question already has an answer here:
How to reverse an object in javascript? [duplicate]
4 answers
I have an object like such:
{ '2018':
{ '12':
{ '25': {},
'26': {},
'27': {},
'28': {},
'29': {},
'30': {},
'31': {} } },
'2019': { '1': { '1': {} } } }
However for frontend purposes, I would like to reverse these values so it shows the most recent dates first. I know that Javascript can't guarantee the order of keys so this isn't even an ideal solution. What is the best way to represent this data so I can iterate over it on the frontend and display the data for each day properly? Thanks
This question already has an answer here:
How to reverse an object in javascript? [duplicate]
4 answers
javascript arrays node.js object
javascript arrays node.js object
asked Jan 3 at 5:30


AntoineAntoine
5,07173651
5,07173651
marked as duplicate by Robert Harvey♦ Jan 3 at 5:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Robert Harvey♦ Jan 3 at 5:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
please share expected out structure.
– Dipak
Jan 3 at 5:34
@Robert Harvey, that question doesn't deal with nested data, besides Javascript doesn't guarantee order with so it's not even a true answer there
– Antoine
Jan 3 at 5:45
@RoberHarvey you did link this question to another closed question which is linked with the order of keys in an object. Maybe his goal was to have anyway a reverted strucutre (as an array) composed by objects in the opposite order
– quirimmo
Jan 3 at 5:48
@quirimmo I've actually designed a solution I'll post it once I confirm it works
– Antoine
Jan 3 at 5:50
add a comment |
please share expected out structure.
– Dipak
Jan 3 at 5:34
@Robert Harvey, that question doesn't deal with nested data, besides Javascript doesn't guarantee order with so it's not even a true answer there
– Antoine
Jan 3 at 5:45
@RoberHarvey you did link this question to another closed question which is linked with the order of keys in an object. Maybe his goal was to have anyway a reverted strucutre (as an array) composed by objects in the opposite order
– quirimmo
Jan 3 at 5:48
@quirimmo I've actually designed a solution I'll post it once I confirm it works
– Antoine
Jan 3 at 5:50
please share expected out structure.
– Dipak
Jan 3 at 5:34
please share expected out structure.
– Dipak
Jan 3 at 5:34
@Robert Harvey, that question doesn't deal with nested data, besides Javascript doesn't guarantee order with so it's not even a true answer there
– Antoine
Jan 3 at 5:45
@Robert Harvey, that question doesn't deal with nested data, besides Javascript doesn't guarantee order with so it's not even a true answer there
– Antoine
Jan 3 at 5:45
@RoberHarvey you did link this question to another closed question which is linked with the order of keys in an object. Maybe his goal was to have anyway a reverted strucutre (as an array) composed by objects in the opposite order
– quirimmo
Jan 3 at 5:48
@RoberHarvey you did link this question to another closed question which is linked with the order of keys in an object. Maybe his goal was to have anyway a reverted strucutre (as an array) composed by objects in the opposite order
– quirimmo
Jan 3 at 5:48
@quirimmo I've actually designed a solution I'll post it once I confirm it works
– Antoine
Jan 3 at 5:50
@quirimmo I've actually designed a solution I'll post it once I confirm it works
– Antoine
Jan 3 at 5:50
add a comment |
1 Answer
1
active
oldest
votes
The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
var array1 = ['one', 'two', 'three'];
console.log('array1: ', array1);
// expected output: Array ['one', 'two', 'three']
var reversed = array1.reverse();
console.log('reversed: ', reversed);
// expected output: Array ['three', 'two', 'one']
/* Careful: reverse is destructive. It also changes
the original array */
console.log('array1: ', array1);
// expected output: Array ['three', 'two', 'one']
OR
//non recursive flatten deep using a stack
var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
function flatten(input) {
const stack = [...input];
const res = ;
while (stack.length) {
// pop value from stack
const next = stack.pop();
if (Array.isArray(next)) {
// push back array items, won't modify the original input
stack.push(...next);
} else {
res.push(next);
}
}
//reverse to restore input order
return res.reverse();
}
flatten(arr1);// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
2
It's not an array.
– Robert Harvey♦
Jan 3 at 5:34
it's an array Please re-check
– Vijendra Yadav
Jan 3 at 14:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
var array1 = ['one', 'two', 'three'];
console.log('array1: ', array1);
// expected output: Array ['one', 'two', 'three']
var reversed = array1.reverse();
console.log('reversed: ', reversed);
// expected output: Array ['three', 'two', 'one']
/* Careful: reverse is destructive. It also changes
the original array */
console.log('array1: ', array1);
// expected output: Array ['three', 'two', 'one']
OR
//non recursive flatten deep using a stack
var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
function flatten(input) {
const stack = [...input];
const res = ;
while (stack.length) {
// pop value from stack
const next = stack.pop();
if (Array.isArray(next)) {
// push back array items, won't modify the original input
stack.push(...next);
} else {
res.push(next);
}
}
//reverse to restore input order
return res.reverse();
}
flatten(arr1);// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
2
It's not an array.
– Robert Harvey♦
Jan 3 at 5:34
it's an array Please re-check
– Vijendra Yadav
Jan 3 at 14:18
add a comment |
The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
var array1 = ['one', 'two', 'three'];
console.log('array1: ', array1);
// expected output: Array ['one', 'two', 'three']
var reversed = array1.reverse();
console.log('reversed: ', reversed);
// expected output: Array ['three', 'two', 'one']
/* Careful: reverse is destructive. It also changes
the original array */
console.log('array1: ', array1);
// expected output: Array ['three', 'two', 'one']
OR
//non recursive flatten deep using a stack
var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
function flatten(input) {
const stack = [...input];
const res = ;
while (stack.length) {
// pop value from stack
const next = stack.pop();
if (Array.isArray(next)) {
// push back array items, won't modify the original input
stack.push(...next);
} else {
res.push(next);
}
}
//reverse to restore input order
return res.reverse();
}
flatten(arr1);// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
2
It's not an array.
– Robert Harvey♦
Jan 3 at 5:34
it's an array Please re-check
– Vijendra Yadav
Jan 3 at 14:18
add a comment |
The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
var array1 = ['one', 'two', 'three'];
console.log('array1: ', array1);
// expected output: Array ['one', 'two', 'three']
var reversed = array1.reverse();
console.log('reversed: ', reversed);
// expected output: Array ['three', 'two', 'one']
/* Careful: reverse is destructive. It also changes
the original array */
console.log('array1: ', array1);
// expected output: Array ['three', 'two', 'one']
OR
//non recursive flatten deep using a stack
var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
function flatten(input) {
const stack = [...input];
const res = ;
while (stack.length) {
// pop value from stack
const next = stack.pop();
if (Array.isArray(next)) {
// push back array items, won't modify the original input
stack.push(...next);
} else {
res.push(next);
}
}
//reverse to restore input order
return res.reverse();
}
flatten(arr1);// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
var array1 = ['one', 'two', 'three'];
console.log('array1: ', array1);
// expected output: Array ['one', 'two', 'three']
var reversed = array1.reverse();
console.log('reversed: ', reversed);
// expected output: Array ['three', 'two', 'one']
/* Careful: reverse is destructive. It also changes
the original array */
console.log('array1: ', array1);
// expected output: Array ['three', 'two', 'one']
OR
//non recursive flatten deep using a stack
var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
function flatten(input) {
const stack = [...input];
const res = ;
while (stack.length) {
// pop value from stack
const next = stack.pop();
if (Array.isArray(next)) {
// push back array items, won't modify the original input
stack.push(...next);
} else {
res.push(next);
}
}
//reverse to restore input order
return res.reverse();
}
flatten(arr1);// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
edited Jan 3 at 5:36
answered Jan 3 at 5:34


Vijendra YadavVijendra Yadav
11
11
2
It's not an array.
– Robert Harvey♦
Jan 3 at 5:34
it's an array Please re-check
– Vijendra Yadav
Jan 3 at 14:18
add a comment |
2
It's not an array.
– Robert Harvey♦
Jan 3 at 5:34
it's an array Please re-check
– Vijendra Yadav
Jan 3 at 14:18
2
2
It's not an array.
– Robert Harvey♦
Jan 3 at 5:34
It's not an array.
– Robert Harvey♦
Jan 3 at 5:34
it's an array Please re-check
– Vijendra Yadav
Jan 3 at 14:18
it's an array Please re-check
– Vijendra Yadav
Jan 3 at 14:18
add a comment |
please share expected out structure.
– Dipak
Jan 3 at 5:34
@Robert Harvey, that question doesn't deal with nested data, besides Javascript doesn't guarantee order with so it's not even a true answer there
– Antoine
Jan 3 at 5:45
@RoberHarvey you did link this question to another closed question which is linked with the order of keys in an object. Maybe his goal was to have anyway a reverted strucutre (as an array) composed by objects in the opposite order
– quirimmo
Jan 3 at 5:48
@quirimmo I've actually designed a solution I'll post it once I confirm it works
– Antoine
Jan 3 at 5:50