I have an array of n number of objects, each which has an array ("needs") in this case. What I want to do is iterate through the array, and grab the sum of the index of each array.
const testData =
[{
location: 'FL',
needs:[{date: '2021-01-01', count: 5},{date: '2021-01-02', count: 1},{date: '2021-01-03', count: 2},{date: '2021-01-04', count: 23},{date: '2021-01-05', count: 65}]
},{
location: 'AL',
needs:[{date: '2021-01-01', count: 1},{date: '2021-01-02', count: 2},{date: '2021-01-03', count: 3},{date: '2021-01-04', count: 4},{date: '2021-01-05', count: 5}]
}]
So in this case, I would be left with an array that looks like [6, 3, 5, 27, 70]
since testData[0].needs[0] + testData[1].needs[0] === 6
& testData[0].needs[1] + testData[1].needs[1] === 3
, etc.
The function I came up with
testData.map((val, index) => {
val.needs.map((needs, index) => {
dayArray.push(needs.count)
})
})
is unfortunately doing testData[0].needs[0] + testData[0].needs[1]
, more or less the opposite of what I need. How do I tweak this function to get the expected results?
question from:
https://stackoverflow.com/questions/65853038/javascript-get-sum-of-indices-of-n-number-of-arrays-of-integers 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…