So below I have manually sorted and loaded my water_supply1 array with data for each year, which ranges from 2015 to 2018 AS COMMENTED , and the way I did it I'm kinder repeating my self so I need a solution to load this data for each year in something like a for loop.
let water_supply1 = []
let sortedData = ["BUF", "CCT", "EKH", "ETK","JHB", "MAN", "NMB", "TSH"]
//data for year : 2015
water_supply1[0] = res.data.table[1].slice(2,3)[0]
water_supply1[1] = res.data.table[5].slice(2,3)[0]
water_supply1[2] = res.data.table[9].slice(2,3)[0]
water_supply1[3] = res.data.table[13].slice(2,3)[0]
water_supply1[4] = res.data.table[17].slice(2,3)[0]
water_supply1[5] = res.data.table[21].slice(2,3)[0]
water_supply1[6] = res.data.table[25].slice(2,3)[0]
water_supply1[7] = res.data.table[29].slice(2,3)[0]
//data for year : 2016
water_supply2[0] = res.data.table[2].slice(2,3)[0]
water_supply2[1] = res.data.table[6].slice(2,3)[0]
water_supply2[2] = res.data.table[10].slice(2,3)[0]
water_supply2[3] = res.data.table[14].slice(2,3)[0]
water_supply2[4] = res.data.table[18].slice(2,3)[0]
water_supply2[5] = res.data.table[22].slice(2,3)[0]
water_supply2[6] = res.data.table[26].slice(2,3)[0]
water_supply2[7] = res.data.table[30].slice(2,3)[0]
//data for year : 2017
water_supply3[0] = res.data.table[3].slice(2,3)[0]
water_supply3[1] = res.data.table[7].slice(2,3)[0]
water_supply3[2] = res.data.table[11].slice(2,3)[0]
water_supply3[3] = res.data.table[15].slice(2,3)[0]
water_supply3[4] = res.data.table[19].slice(2,3)[0]
water_supply3[5] = res.data.table[23].slice(2,3)[0]
water_supply3[6] = res.data.table[27].slice(2,3)[0]
water_supply3[7] = res.data.table[31].slice(2,3)[0]
//data for year : 2018
water_supply4[0] = res.data.table[4].slice(2,3)[0]
water_supply4[1] = res.data.table[8].slice(2,3)[0]
water_supply4[2] = res.data.table[12].slice(2,3)[0]
water_supply4[3] = res.data.table[16].slice(2,3)[0]
water_supply4[4] = res.data.table[20].slice(2,3)[0]
water_supply4[5] = res.data.table[24].slice(2,3)[0]
water_supply4[6] = res.data.table[28].slice(2,3)[0]
water_supply4[7] = res.data.table[32].slice(2,3)[0]
So this is how I tried doing it , but the problem with this solution it only takes the first year which is 2015 and I want to loop through and load data for all the years.
let num = 0
let no = 1
for (let i = 0; i < sortedData.length; i ) {
no = no num
array[i] = res.data.table[no].slice(2, 3)[0]
num = 4
}
console.log('check for loop',array)
result : [87.7932138127171, 96.0160356511943, 97.238574836612, 95.0888864347421, 97.0599409338406, 98.063090378178, 92.2282248644469, 86.421762925966]
CodePudding user response:
Why not using a 2D array, where the first dimension has the year and the second one your data? For example:
yearMapper = {
2015: 1,
2016: 2,
2017: 3,
2018: 4
}
let waterSupplyCollection = [];
for (let year = 2015; year < 2020; year ){
let waterSupply = [];
for (let i = 0; i<8; i ){
let index = i*4 yearMapper[year];
waterSupply.push(res.data.table[index].slice(2,3)[0]);
}
waterSupplyCollection.push(waterSupply);
}
In this case, there is a really simple pattern to compute the index depending on the different years, such that you don't need to assign everything manually.