I'm trying to create a function that will convert an array like this:
[['a', 1], ['b', 2], ['c', [['d', 4]]]]
Into an object like this:
{ a: 1, b: 2, c: { d: 4 } }
So far I just have a function that converts a regular array to regular object, and I'm getting the hang of recursion I'm just not sure how to implement it in such a case. Here is the function that I am working with:
const deepArrayToObject = function(arr) {
let obj = {};
if (arr !== []) {
return obj;
}
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
let key = arr[i][0];
let value = arr[i][1];
obj[key] = value;
}
}
return obj;
};
I appreciate the feedback.
[['d', 4]]. Is that intentional?