I created a nested object that holds the following format:
var lineMap = {
2016: {
Hemoglobin: 33 ,
Lysozyme: 33 ,
Myoglobin: 47 ,
DNA: 13 ,
Cytochrome C: 33
},
2017: {
Hemoglobin: 8 ,
Lysozyme: 47 ,
Myoglobin: 8 ,
DNA: 12 ,
Cytochrome C: 33
},
2018: {
Hemoglobin: 8 ,
Lysozyme: 33 ,
Myoglobin: 47 ,
DNA: 12 ,
Cytochrome C: 13
},
2019: {
Hemoglobin: 8 ,
Lysozyme: 8 ,
Myoglobin: 47 ,
DNA: 8 ,
Cytochrome C: 47
}
}
And I'd like to place each year's item count into its own index of an array such that:
var arr = [
[33, 33, 47, 13, 33],
[8, 47, 8, 12, 33],
[8, 33, 47, 12, 13],
[8, 8, 47, 8, 47]
]
I have tried with creating a nested for loop to iterate through the nested object lineMap
.
for (var i = 0; i < year.length; i ++){
for (var j = 0; j < itemName.length; j++){
temp_arr[j] = lineMap[year[i]][itemName[j]];
}
console.log("Index: " + i + "\n" + temp_arr);
arr[i] = temp_arr;
}
console.log(arr);
At the 5th line (console.log(temp_arr)
), the console printed out what I expected--an array of the item count of its respective iteration:
'Index: 0
33,33,47,13,33'
'Index: 1
8,47,8,12,33'
'Index: 2
8,33,47,12,13'
'Index: 3
8,8,47,8,47'
However, at the 8th line (console.log(arr)
), I am not receiving my expected output. Instead, I am getting:
var arr = [
[8, 8, 47, 8, 47],
[8, 8, 47, 8, 47],
[8, 8, 47, 8, 47],
[8, 8, 47, 8, 47]
]