i have the Array in js, but how to convert to object with nesting childs ?
This is my array:
const arr = [{ no: 1 }, { no: 2 }, { no: 3 }];
this is output my expected:
// output
const obj = { no: 1, nest: [{ no: 2, nest: [{ no: 3 }] }] };
i have the Array in js, but how to convert to object with nesting childs ?
This is my array:
const arr = [{ no: 1 }, { no: 2 }, { no: 3 }];
this is output my expected:
// output
const obj = { no: 1, nest: [{ no: 2, nest: [{ no: 3 }] }] };
3
Answers
You can do it by
arr.reduceRight
.Here is an example –
output:
using
reduce
You want to
right reduce
your array. With reference to this Array.prototype.reduceRight()Example:
Output: