admin管理员组文章数量:1357562
I have an array [1, 2, 3]
and I want to transfer it to object with nested parent-child objects's series like this :
{ value: 1, rest: { value: 2, rest: { value: 3, rest: null } }
If I have an array [1, 2, 3, 4]
the result will be like this :
{ value: 1, rest: { value: 2, rest: { value: 3, rest: { value:4, rest:null } }
The best effort of me is this snippet of code :
const arrayToList = (array) => {
let list = { value: null, rest: null };
for (let e of array) {
array.indexOf(e) === 0 && (list.value = e);
array.indexOf(e) >= 1 && (list.rest = { value: e });
}
return list;
};
console.log(arrayToList([1, 2, 3]));
I have an array [1, 2, 3]
and I want to transfer it to object with nested parent-child objects's series like this :
{ value: 1, rest: { value: 2, rest: { value: 3, rest: null } }
If I have an array [1, 2, 3, 4]
the result will be like this :
{ value: 1, rest: { value: 2, rest: { value: 3, rest: { value:4, rest:null } }
The best effort of me is this snippet of code :
const arrayToList = (array) => {
let list = { value: null, rest: null };
for (let e of array) {
array.indexOf(e) === 0 && (list.value = e);
array.indexOf(e) >= 1 && (list.rest = { value: e });
}
return list;
};
console.log(arrayToList([1, 2, 3]));
Share
Improve this question
asked Jun 20, 2020 at 0:07
Ala Eddine MenaiAla Eddine Menai
2,8907 gold badges30 silver badges56 bronze badges
3
- did you see my answer ? if there's something went wrong with it please let me know – Boussadjra Brahim Commented Aug 14, 2020 at 20:14
- 1 Yes. It works well. – Ala Eddine Menai Commented Aug 14, 2020 at 20:43
- Thank you my bro ! – Boussadjra Brahim Commented Aug 14, 2020 at 20:54
4 Answers
Reset to default 8You can use reduceRight
like so:
let obj = arr.reduceRight((rest, value) => ({ value, rest }), null);
It starts building the object from the inside out; it starts by creating the innermost object and then it uses that object as the rest
property for the next outer object and so on until there are no more items in the array.
Demo:
let obj = [1, 2, 3, 4].reduceRight((rest, value) => ({ value, rest }), null);
console.log(obj);
You can create such object by running below recursive function:
let arr = [1, 2, 3, 4];
let transform = (arr, obj) => {
if(arr.length === 0){
return obj;
} else {
let last = arr[arr.length - 1];
let newArr = arr.slice(0, arr.length - 1);
return transform(newArr, { value: last, rest: obj || null })
}
};
console.log(transform(arr));
Use a recursive function:
let array = [1, 2, 3];
function arrayToL(array) {
let el = array.splice(0, 1)[0];
let rtn = {
value: el
}
rtn.rest = (array.length > 0) ? arrayToL(array) : null;
return rtn;
}
console.log(arrayToL(array));
I suggest another solution using the spread operator
and reversing the array and start building object from the array end :
let arr = [1, 2, 4, 5]
let obj = {} //object to be built
arr.slice().reverse().forEach(item => { //i used the slice method
//in order to avoid mutating
//the original variable
obj = { ...obj,
...{
value: item,
rest: obj
}
};
})
console.log(obj)
本文标签: javascriptHow to create Object with nested Objects from an ArrayStack Overflow
版权声明:本文标题:javascript - How to create Object with nested Objects from an Array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743973720a2570792.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论