I have a nested object, how do I have function that you pass in the object and a key and get back the value?
Example Inputs
object = {"a1":{"b1":"{"c1":"d1"}"}}
key = a1/b1/c1
object = {"x1":{"y1":"{"z1":"a1"}"}}
key = x1/y1/z1
value = a1
Below is what I have attempted but it's wrong
var obj, traverse;
obj = {
a1: {
b1: c1,
b1: d1
},
x1: {
y1: z1,
y1: a1
}
};
traverse = function(node, path) {
var pairs;
if (!(pairs = _(node).pairs()).length) {
return [
{
keys: path,
value: node
}
];
} else {
return [].concat.apply([], _(pairs).map(function(kv) {
return traverse(kv[1], path.concat(kv[0]));
}));
}
};
console.log(traverse(obj, []));