Currently, I am working on a function that converts a 3D array into an array of key/value pairs. Below is the code I have written.
function arrToDictArray(array) {
var myarr = [];
var mydic = {};
var x = 0;
for (var i in array) {
console.log("i: " + i);
var x = array[i];
for (var j in x) {
console.log("j: " + j);
mydic[array[i][j][0]] = array[i][j][1];
console.log(mydic[array[i][j][0]]);
}
myarr.push(mydic);
}
console.log(myarr);
return myarr;
}
My expected output for the new array was:
[{name:'Mauri', salary: 100000, age:40},{name: 'felicia', salary: 120000, age:36}]
However, I ended up with duplicate entries for "felicia". Instead of the desired output, I got this:
[{name: 'felicia', salary: 120000, age:36},{name: 'felicia', salary: 120000, age:36}]
I tried adjusting the position of the myarr.push(mydic)
method within the loops, but the issue persisted. It seems like something is causing the data to get overwritten, even though I'm using .push()
.
Here's a screenshot of the output I am getting.