What is Reverse object hierarchy
-
is there any way to reverse the object in js?
const initObject = { value: 5, next: { value: 10, next: { value: 15 next: null } }, } //expected result const newObject = { value: 15, next: { value: 10, next: { value: 5, next: null } } }need to a function, but struggling hard.
I tried to find the object depth-first and then make a founded amount of iterations for … in … inside the object but don’t know how to rewrite the new one -
is there any way to reverse the object in js?
const initObject = { value: 5, next: { value: 10, next: { value: 15 next: null } }, } //expected result const newObject = { value: 15, next: { value: 10, next: { value: 5, next: null } } }need to a function, but struggling hard.
I tried to find the object depth-first and then make a founded amount of iterations for … in … inside the object but don’t know how to rewrite the new one@maria-irshad said in What is Reverse object hierarchy:
is there any way to reverse the object in js?
const initObject = { value: 5, next: { value: 10, next: { value: 15 next: null } }, } //expected result const newObject = { value: 15, next: { value: 10, next: { value: 5, next: null } } }need to a function, but struggling hard.
I tried to find the object depth-first and then make a founded amount of iterations for … in … inside the object but don’t know how to rewrite the new oneTry this
const initObject = { value: 5, next: { value: 10, next: { value: 15, next: null } } } const getValues = ({ value, next }) => next ? [value, ...getValues(next)] : [value] const createObject = values => values.reduce((next, value) => ({ value, next }), null) const output = createObject(getValues(initObject)) console.log(output)
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login
