I have an object
{
1: {id: 1, first: 1, last: 5}
2: {id: 2, first: 6, last: 10}
3: {id: 3, first: 11, last: 15}
}
I need to reverse the items order without sorting the keys so the final result is:
{
1: {id: 3, first: 11, last: 15}
2: {id: 2, first: 6, last: 10}
3: {id: 1, first: 1, last: 5}
}
Is this possible?
I tried to convert it into array and then into an object but the new object starts with key 0 while I need it to start with key 1:
let array = [];
Object.values(this.props.items)
.sort()
.reverse()
.forEach(function(b) {
array.push(b);
});
const newItems = Object.assign({}, array);
// Result:
{
0: {id: 3, first: 11, last: 15}
1: {id: 2, first: 6, last: 10}
2: {id: 1, first: 1, last: 5}
}
EDIT
Worth mention that my object is typed:
Btw this.props.items
is typed TypeScript
object eg. Section.Item[]