I need to create a javascript object using values stored in an array. Every value should be a new key inside the previous one. What would be the best approach to achieve this?
我需要使用存储在数组中的值创建一个javascript对象。每个值都应该是前一个值中的新键。实现这一目标的最佳方法是什么?
var option = ['level_1','level_2','level_3','level_4'];
$.each( option, function( key, value ) {
// ....
});
// I'm trying to get this result
var result = {
'level_1': {
'level_2': {
'level_3': {
'level_4':{}
}
}
}
}
4 个解决方案
#1
2
You have to keep track of where to put the next key. So, create a variable and initially set it to result
, then on each pass through the array, move where that variable points to.
您必须跟踪下一个键的放置位置。因此,创建一个变量并将其初始设置为结果,然后在每次通过数组时,移动该变量指向的位置。
var option = ['level_1','level_2','level_3','level_4'];
var result = {};
var nextKeyGoesHere = result;
option.forEach( function( value ) {
nextKeyGoesHere[value] = {};
nextKeyGoesHere = nextKeyGoesHere[value];
});
console.log(result);
#2
6
You can use reduceRight
for this, with the ES6 computed property name syntax.
您可以使用reduceRight,使用ES6计算属性名称语法。
const option = ['level_1','level_2','level_3','level_4'];
const obj = option.reduceRight( (acc, lvl) => ({ [lvl]: acc }), {});
console.log(obj);
In traditional function syntax it would be:
在传统的函数语法中,它将是:
const obj = option.reduceRight(function (acc, lvl) {
return { [lvl]: acc };
}, {});
#3
1
Can use Array#reduce()
可以使用Array#reduce()
var option = ['level_1','level_2','level_3','level_4'];
var res = {};
option.reduce((o, key) => (o[key] = {} , o[key]), res)
console.log(res)
#4
0
you can use any of the other answers that use Array#reduce
, however, if you'd like a recursive version here it is:
你可以使用任何其他使用Array#reduce的答案,但是,如果你想要一个递归版本,它是:
function _makeTree(arr, index, subtree){
if(index < arr.length){
subtree[arr[index]] = {};
return _makeTree(arr, index+1, subtree[arr[index]])
}
else return;
}
function makeTree(arr){
var tree = {};
_makeTree(arr, 0, tree)
return tree;
}
var arr = ['level_1','level_2','level_3','level_4'];
console.log(makeTree(arr));
#1
2
You have to keep track of where to put the next key. So, create a variable and initially set it to result
, then on each pass through the array, move where that variable points to.
您必须跟踪下一个键的放置位置。因此,创建一个变量并将其初始设置为结果,然后在每次通过数组时,移动该变量指向的位置。
var option = ['level_1','level_2','level_3','level_4'];
var result = {};
var nextKeyGoesHere = result;
option.forEach( function( value ) {
nextKeyGoesHere[value] = {};
nextKeyGoesHere = nextKeyGoesHere[value];
});
console.log(result);
#2
6
You can use reduceRight
for this, with the ES6 computed property name syntax.
您可以使用reduceRight,使用ES6计算属性名称语法。
const option = ['level_1','level_2','level_3','level_4'];
const obj = option.reduceRight( (acc, lvl) => ({ [lvl]: acc }), {});
console.log(obj);
In traditional function syntax it would be:
在传统的函数语法中,它将是:
const obj = option.reduceRight(function (acc, lvl) {
return { [lvl]: acc };
}, {});
#3
1
Can use Array#reduce()
可以使用Array#reduce()
var option = ['level_1','level_2','level_3','level_4'];
var res = {};
option.reduce((o, key) => (o[key] = {} , o[key]), res)
console.log(res)
#4
0
you can use any of the other answers that use Array#reduce
, however, if you'd like a recursive version here it is:
你可以使用任何其他使用Array#reduce的答案,但是,如果你想要一个递归版本,它是:
function _makeTree(arr, index, subtree){
if(index < arr.length){
subtree[arr[index]] = {};
return _makeTree(arr, index+1, subtree[arr[index]])
}
else return;
}
function makeTree(arr){
var tree = {};
_makeTree(arr, 0, tree)
return tree;
}
var arr = ['level_1','level_2','level_3','level_4'];
console.log(makeTree(arr));