SOLVED 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 -
@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)
-
-
@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)



100% Off on Your FEE Join US! Ask Me How?


