I have been attempting to transform this complex array into an object. However, I am facing an issue where only the second part of the array is being saved in the object, while the first part is missing.
Is there a way to ensure that the entire array gets stored in the object?
var data = [
[
['name', 'John Smith'],
['age', 34],
['occupation', 'nurse']
],
[
['name', 'Nico Klein'],
['age', 24],
['occupation', 'engineer']
]
];
function convertToObject(arr) {
var obj = {};
for (var j = 0; j < arr.length; j++) {
for (var i = 0; i < arr[j].length; i++) {
obj[arr[j][i][0]] = arr[j][i][1];
}
}
return obj;
}
var finalResult = convertToObject(data);
console.log(finalResult);
Additionally, are there more efficient ways to write this conversion code?