Currently writing a function that takes a 3D array and makes the values into an array of key/value pairs. Below is my code.
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);
//console.log(myarr[i]);
}
console.log(myarr);
return myarr;
}
I was expecting for my new array to show
[{name:'Mauri', salary: 100000, age:40},{name: 'felicia', salary: 120000, age:36}]
but instead I get felicia duplicated.
[{name: 'felicia', salary: 120000, age:36},{name: 'felicia', salary: 120000, age:36}]
I tried to change my myarr.push(mydic) method in the j loop and outside of the i and j loop entirely but it still gets overwritten. I can't seem to see why a .push() is overwriting anything.
Screenshot of my output.