For example, a function that would be const arrToNestObj = arr => {}
and would get [a, b, c, d, e]
and return
a: {
b: {
c: {
d: {
e: {
}
}
}
}
}
Thanks!
Use Array.reduceRight()
to create a nested object:
const arrToNestObj = arr => arr.reduceRight((acc, key) => ({ [key]: acc }), {}) const arr = ['a', 'b', 'c', 'd', 'e'] const result = arrToNestObj(arr) console.log(result)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.