我有一个包含许多嵌套对象的扩展JS对象,例如:
data = {
sundriesIncreases : {
month1: { projectedIncrease: 1.3, actualIncrease: 1.3 },
month2: { projectedIncrease: 1.6, actualIncrease: 1.5 },
month3: { projectedIncrease: 1.4, actualIncrease: 1.4 },
month4: { projectedIncrease: 2, actualIncrease: 2.1 },
month5: { projectedIncrease: 1.2, actualIncrease: 1.3 },
//etc
},
//etc
}
我正在使用一个循环遍历不同的数据集,我想使用该循环的索引从数组中检索相应的月份数据。
然而,我很难弄清楚如何通过索引获取子数组的给定值。
到目前为止,我一直在尝试按索引获取它:
customerNumbers.forEach((amt, index) => {
let actInc = data.sundriesIncreases[index].actualIncrease;
console.log(`amt=${val} index=${index} increase=${actInc}`)
});
并将父对象转换为数组:
var increasesArr = Object.entries(data.sundriesIncreases);
customerNumbers.forEach((amt, index) => {
let actInc = increasesArr[index].actualIncrease;
console.log(`amt=${val} index=${index} increase=${actInc}`)
});
但到目前为止,一切都不起作用。
当然
customerNumbers.forEach((amt, index) => {
if (index == 0) {
let actInc = data.sundriesIncreases.month1.actualIncrease;
console.log(`amt=${val} index=${index} increase=${actInc}`)
}
});
有效,但这并不理想。
在谷歌上搜索这个问题揭示了很多关于如何获得
钥匙
通过
价值
注意到很多关于如何做相反的事情。
有人能给我指出正确的方向吗?