Objective:
I am developing an app using AngularJS and Lodash. I need a function that do the following transformation:
From
{
a: 0,
b: {x: {y: 1, z: 2}},
c: 3
}
into
{
a: 0,
'b.x.y': 1,
'b.x.z': 2,
c: 3
}
Right now, I am using a recursive function to traverse through the object. For the time being, my code looks like that (incomplete):
var obj = {},
keys, property, value;
var recur = function (mat) {
keys = Object.keys(mat);
for (var i = 0; i < keys.length; i++) {
property = keys[i];
value = mat[property];
if (isObj(value)) {
//debugger;
recur(value);
} else {
obj[property] = value;
}
}
}
recur({
a: 0,
b: {x: {y: 1, z: 2}},
c: 3
});
console.log(obj);
function isObj(value) {
return typeof value === "object";
}
One issue that I am getting while debugging is:
i is not being incremented; during the highest-level recursion call, i remains 0.
Anyone knows why? And if you know of any other way for doing this transformation, please share it.
Kind Regards.