0

I apparently cannot think abstractly enough to do this... but I'd like to create a Javascript object out of an array that uses the array values as property names, but they should be nested objects within each other.

So if I had an array like this:

['First', 'Second', 'Third', 'Fourth']

My expected output would be:

{
 First: {
 Second: {
 Third: {
 Fourth: {}
 }
 }
 }
}

UPDATE Here is the function I was using as mentioned in the commment:

function appendKey(obj, to, key) {
 if (obj.hasOwnProperty(to)) {
 appendKey(obj[to], to, key);
 } else {
 obj[key] = {};
 }
 return obj;
}

My intent was to call it as such:

var data = ['First', 'Second', 'Third', 'Fourth'];
data = appendKey(data, 'First', 'Second');
data = appendKey(data, 'Second', 'Third');
data = appendKey(data, 'Third', 'Fourth');

Clearly that could be put into a loop, which is why I wanted to do it that way. My output ended up being:

data = { 'First' : { 'Second' } } // it works this time!
data = { 'First' : { 'Second' },
 'Third' : { } }
data = { 'First' : { 'Second' },
 'Third' : { 'Fourth' { } } } 
asked Feb 21, 2013 at 16:55
4
  • 1
    What have you tried? Commented Feb 21, 2013 at 16:56
  • I tried a self-calling function that takes in the object as-is and the key you want to add. If the key doesnt exist, add it to the passed object and return it. If the key exists, it calls itself again but with the object.key in the first parameter. Commented Feb 21, 2013 at 17:02
  • @HonusWagner: What did it do, and what didn’t it do? Could you show us the code? Commented Feb 21, 2013 at 17:03
  • I think my approach would work if the object was by value rather than reference. Commented Feb 21, 2013 at 17:04

2 Answers 2

5

Outside the loop, store your base object in a variable, and have a separate variable to store the current object (which starts off being the same as the base).

Inside the loop, take the "current" object, give it a key using the current array member, assign a new object to that key, and make that new object the new "current" object.

var arr = ['First', 'Second', 'Third', 'Fourth'],
 obj = {},
 currObj = obj;
arr.forEach(function(key) {
 currObj = (currObj[key] = {});
});
console.log(obj); // {First:{Second:{Third:{Fourth:{}}}}}

DEMO: http://jsfiddle.net/qunDt/


You can unwind the code in the forEach a bit if you prefer.

arr.forEach(function(key) {
 currObj[key] = {};
 currObj = currObj[key];
});

If you wanted a purely recursive approach, you could do it like this:

function nested(list, o) {
 if (list.length === 0)
 return o;
 o[list.shift()] = nested(list, {})
 return o;
}
var obj = nested(arr, {});
answered Feb 21, 2013 at 17:03
Sign up to request clarification or add additional context in comments.

Comments

1
var names = ['First', 'Second', 'Third', 'Fourth'];
var build = function(soFar,remaining) {
 if(remaining.length === 0) {return soFar;}
 var outer = {};
 outer[names.pop()] = soFar;
 return build(outer, names);
}
var result = build({}, names);
answered Feb 21, 2013 at 17:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.