This sounds like a simple task, but I can't quite figure it out: I have an array :
这听起来像一个简单的任务,但我无法弄清楚:我有一个数组:
var array = ['opt1','sub1','subsub1','subsubsub1']
From that I want to generate the following objects:
从那我想生成以下对象:
{
opt1:{
sub1:{
subsub1:{
subsubsub1:{}
}
}
}
}
I have a way to do it, making a string and using eval, but I'm looking to avoid that, any idea?
我有办法做到这一点,制作一个字符串并使用eval,但我希望避免这种情况,任何想法?
4 个解决方案
#1
23
You could use reduce
:
你可以使用reduce:
var array = ['opt1','sub1','subsub1','subsubsub1'];
var object = {};
array.reduce(function(o, s) { return o[s] = {}; }, object);
console.log(object);
But this was only introduced in ECMAScript 5.1, so it won't be supported in some older browsers. If you want something that will be supported by legacy browsers, you could use the polyfill technique described in the MDN article above, or a simple for
-loop, like this:
但这仅在ECMAScript 5.1中引入,因此在某些旧版浏览器中不支持它。如果您想要旧版浏览器支持的内容,您可以使用上面MDN文章中描述的polyfill技术,或者使用简单的for循环,如下所示:
var array = ['opt1','sub1','subsub1','subsubsub1'];
var object = {}, o = object;
for(var i = 0; i < array.length; i++) {
o = o[array[i]] = {};
}
console.log(object);
#3
3
You can use reduceRight
to transform the array into a 'chain' of objects:
您可以使用reduceRight将数组转换为对象的“链”:
const array = ['a', 'b', 'c'];
const object = array.reduceRight((obj, next) => ({[next]: obj}), {});
// Example:
console.log(object); // {"a":{"b":{"c":{}}}}
#4
0
You can use the following Function
function arr2nestedObject(myArray){
var cp_myArray = myArray;
var lastobj = {};
while(cp_myArray.length>0){
newobj = {};
var prop = cp_myArray.pop();
newobj[prop] = lastobj;
lastobj = newobj;
}
return lastobj;
}
The following code:
以下代码:
var myArray = ["personal-information", "address", "street",'Great-Success'];
console.log(JSON.stringify(arr2nestedObject(myArray),undefined,2));
Would Produce the Following Output:
会产生以下输出:
{
"personal-information": {
"address": {
"street": {
"Great-Success": {}
}
}
}
}
Please let me know if that was what you meant.
Kind Regards.
亲切的问候。
#1
23
You could use reduce
:
你可以使用reduce:
var array = ['opt1','sub1','subsub1','subsubsub1'];
var object = {};
array.reduce(function(o, s) { return o[s] = {}; }, object);
console.log(object);
But this was only introduced in ECMAScript 5.1, so it won't be supported in some older browsers. If you want something that will be supported by legacy browsers, you could use the polyfill technique described in the MDN article above, or a simple for
-loop, like this:
但这仅在ECMAScript 5.1中引入,因此在某些旧版浏览器中不支持它。如果您想要旧版浏览器支持的内容,您可以使用上面MDN文章中描述的polyfill技术,或者使用简单的for循环,如下所示:
var array = ['opt1','sub1','subsub1','subsubsub1'];
var object = {}, o = object;
for(var i = 0; i < array.length; i++) {
o = o[array[i]] = {};
}
console.log(object);
#2
#3
3
You can use reduceRight
to transform the array into a 'chain' of objects:
您可以使用reduceRight将数组转换为对象的“链”:
const array = ['a', 'b', 'c'];
const object = array.reduceRight((obj, next) => ({[next]: obj}), {});
// Example:
console.log(object); // {"a":{"b":{"c":{}}}}
#4
0
You can use the following Function
function arr2nestedObject(myArray){
var cp_myArray = myArray;
var lastobj = {};
while(cp_myArray.length>0){
newobj = {};
var prop = cp_myArray.pop();
newobj[prop] = lastobj;
lastobj = newobj;
}
return lastobj;
}
The following code:
以下代码:
var myArray = ["personal-information", "address", "street",'Great-Success'];
console.log(JSON.stringify(arr2nestedObject(myArray),undefined,2));
Would Produce the Following Output:
会产生以下输出:
{
"personal-information": {
"address": {
"street": {
"Great-Success": {}
}
}
}
}
Please let me know if that was what you meant.
Kind Regards.
亲切的问候。