# Js partly reduce two arrays

**URL:** <https://community.homey.app/t/js-partly-reduce-two-arrays/73674>\
**Category:** Developers\
**Created:** [December 16, 2022, 8:25am UTC](https://community.homey.app/t/js-partly-reduce-two-arrays/73674 "2022-12-16T08:25:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [December 16, 2022, 8:25am UTC](https://community.homey.app/t/js-partly-reduce-two-arrays/73674/1 "2022-12-16T08:25:49Z")

</div>

I have two arrays:  
arr1 = [60,70,80,80,100,110,120,130,140,150,100,120]

arr2 = [60,70,80,80,100,110,120,130,140,150,100,120]

I want to count those arrays, but… in case the initial mont in my script is 8, array1 has to count from index 7 till 11 and array2 count index 0 till 6. The result of these two must also be counted. The result i suppose concat?

The initial month can differ.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [December 16, 2022, 8:29am UTC](https://community.homey.app/t/js-partly-reduce-two-arrays/73674/2 "2022-12-16T08:29:28Z")

</div>

_Just_ for month 8, or do you mean for every month X you want to start counting from (X - 1) to 11 from `arr1` and 0 to (X - 2) from `arr2`?

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [December 16, 2022, 8:31am UTC](https://community.homey.app/t/js-partly-reduce-two-arrays/73674/3 "2022-12-16T08:31:46Z")

</div>

Yes i want to count index 7,8,9,10 and 11 from array1 and 0,1,2,3,4,5 and 6 from array2. And count those two results

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [December 16, 2022, 8:33am UTC](https://community.homey.app/t/js-partly-reduce-two-arrays/73674/4 "2022-12-16T08:33:00Z")

</div>

But only for month 8? What should happen for the other months?

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [December 16, 2022, 8:43am UTC](https://community.homey.app/t/js-partly-reduce-two-arrays/73674/5 "2022-12-16T08:43:37Z")

</div>

Array1are the monthly results of the peak performance from my solar panels in 2022. Array2 are the monthly results of the peak performance from my solar panels in 2023. The energy company bills from august-july. So i want the monthly peak performance from august 2022 till july 2023. This also means in december there is no array2. Array2 will be made on 1 janauary 2023.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [December 16, 2022, 8:02pm UTC](https://community.homey.app/t/js-partly-reduce-two-arrays/73674/6 "2022-12-16T20:02:16Z")

</div>

Is this going the right direction, assuming that “global.get(‘energyContractYear’);” has been set the first time to currentYear with "global.set(‘energyContractYear’, +currentYear)

```auto
var energyContractMonth = '8';

global.get('energyContractYear');

if(energyContractYear === currentYear)
{
var tmpPeakContractPeriodMonth = global.get('peakContractPeriodMonth' +currentYear);
var tmp = tmpPeakContractPeriodMonth.slice(energyContractMonth -1,tmpPeakContractPeriodMonth.length);
global.set('PeakContractPeriodMonth' +currentYear, tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
peakContractPeriodYear.splice(currentYear -1,1,tmp1);
global.set('PeakContractPeriodYear', peakContractPeriodYear);
}
{
var tmpPeakContractPeriodMonth = global.get('peakContractPeriodMonth' +currentYear);
var tmp = tmpPeakContractPeriodMonth.slice(0,energyContractMonth -2);
global.set('peakContractPeriodMonth' +currentYear, tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
tmp2 = global.get('PeakContractPeriodYear');
var tmp2 = tmp1 + tmp2[currentYear -2];
peakContractPeriodYear.splice(currentYear -2,1,tmp2);
global.set('peakContractPeriodYear', peakContractPeriodYear);
}

if(currentmonth -1 === energyContractMonth - 2)
{
global.set('energyContractYear', +currentYear)
}

```

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [December 18, 2022, 2:33pm UTC](https://community.homey.app/t/js-partly-reduce-two-arrays/73674/7 "2022-12-18T14:33:53Z")

</div>

This how far i am. Looks difficult, to me. Maybe there is an easier way?

```auto
var indexMonth = [1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12]

var indexYear = [currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,]

var daysInCurrentMonth = getDaysInMonth(+indexYear[(currentMonth -1) +12], +indexMonth[(currentMonth -1) +12]);
console.log('49 days in currentMonth is: '+daysInCurrentMonth)
var indexThisMonthDay = Array.from({length: +daysInCurrentMonth}, (_, i) => i + 1)
console.log('50 indexThisMonthDay is: '+indexThisMonthDay)

var daysInPreviousMonth = getDaysInMonth(+indexYear[(currentMonth -2) +12], +indexMonth[(currentMonth -2) +12]);
console.log('51 days in previousMonth is: '+daysInPreviousMonth)

indexPreviousMonthDay = Array.from({length: +daysInPreviousMonth}, (_, i) => i + 1)
console.log('52 indexPreviousMonthDay is: '+indexPreviousMonthDay)

var indexDay = indexPreviousMonthDay.concat(indexThisMonthDay);
console.log('53 indexDay is: '+indexDay)
console.log('54 daysInCurrentMonth is: '+daysInCurrentMonth)

var tmpPeakDay = global.get('peakDay' +indexYear[((currentMonth -1) +12)] +indexMonth[((currentMonth -1) +12)]);
tmpPeakDay.splice(+indexDay[((+daysInPreviousMonth -1) +currentDay -1)],1,peak);
global.set('peakDay'+indexYear[((currentMonth -1) +12)] +indexMonth[((currentMonth -1) +12)], tmpPeakDay);
console.log('67 peakDay'+indexYear[((currentMonth -1) +12)] +indexMonth[((currentMonth -1) +12)] +' is ' +tmpPeakDay);
var peakDay = tmpPeakDay[currentDay - 1];
console.log('68 peakDay is ' +peakDay);
var tmpPeakPreviousDay = global.get('peakDay' +indexYear[((currentMonth -2) +12)] +indexMonth[((currentMonth -2) +12)]);
console.log('69 tmpPeakPreviousDay' +indexYear[((currentMonth -2) +12)] +indexMonth[((currentMonth -2) +12)] +' is ' +tmpPeakPreviousDay);
var peakPreviousDay = tmpPeakPreviousDay[+indexDay[(((+daysInPreviousMonth -1) +currentDay -1) -1)]];
console.log('70 peakPreviousDay is ' +peakPreviousDay);
var peakDayResult = peakDay -peakPreviousDay
console.log('71 peakDayResult is: ' +peakPreviousDay +' - ' +peakDay +' is ' +tmpPeakDay);

var tmpPeakMonth = global.get('peakMonth' +indexYear[((currentMonth -1) +12)]);
console.log('72 tmpPeakMonth'+indexYear[((currentMonth -1) +12)] +' is: ' +tmpPeakMonth);
var tmpPeakPreviousMonth = global.get('peakMonth' +indexYear[((currentMonth -2) +12)]);
console.log('73 tmpPeakPreviousMonth'+indexYear[((currentMonth -1) +12)] +' is: ' +tmpPeakPreviousMonth);
tmpPeakMonth.splice(+indexYear[((currentMonth -1) +12)],1,peakDay - tmpPeakPreviousMonth[+indexMonth[((currentMonth -2) +12)]]);
global.set('peakMonth'+indexYear[((currentMonth -1) +12)], tmpPeakMonth);
console.log('74 peakMonth' +indexYear[((currentMonth -1) +12)] +' is ' +tmpPeakMonth);

var energyContractMonth = '8';

var energyContractYear = global.get('energyContractYear');
console.log('77 energyContractYear is: '+energyContractYear)
if(energyContractYear === currentYear)
{
var tmpPeakContractPeriodYear = global.get('peakContractPeriodYear')
var tmpPeakContractPeriodMonth = global.get('peakContractPeriodMonth' +currentYear);
var tmp = tmpPeakContractPeriodMonth.slice(energyContractMonth -1,tmpPeakContractPeriodMonth.length);
global.set('PeakContractPeriodMonth' +currentYear, tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
tmpPeakContractPeriodYear.splice(currentYear -1,1,tmp1);
global.set('PeakContractPeriodYear', tmpPeakContractPeriodYear);
}
{
var tmpPeakContractPeriodYear = global.get('peakContractPeriodYear')
var tmpPeakContractPeriodMonth = global.get('peakContractPeriodMonth' +currentYear);
var tmp = tmpPeakContractPeriodMonth.slice(0,energyContractMonth -2);
global.set('peakContractPeriodMonth' +currentYear, tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
tmp2 = global.get('PeakContractPeriodYear');
var tmp2 = tmp1 + tmp2[currentYear -2];
tmpPeakContractPeriodYear.splice(currentYear -2,1,tmp2);
global.set('peakContractPeriodYear', tmpPeakContractPeriodYear);
}

var tmpPeakYear = global.get('peakYear');
var tmpPeakMonth = global.get('peakMonth'+indexYear[((currentMonth -1) +12)]);
var tmp = tmpPeakMonth.reduce((partialSum, a) => partialSum + a, 0);
tmpPeakYear.splice(+indexYear[((currentMonth -1) +12)] -2001,1,tmp);
global.set('peakYear',tmpPeakYear );
console.log('78 peakYear is ' +tmpPeakYear);

if(currentMonth -1 === energyContractMonth - 2)
{
global.set('energyContractYear', +currentYear)
}

tmpPeakContractPeriodYear.splice(currentYear -2001,1,meterKwhThisYear_PBTH);
tmpPeakContractPeriodYear.splice(currentYear -2002,1,meterKwhPreviousYear_PBTH);
global.set('peakContractPeriodYear', tmpPeakContractPeriodYear);
console.log('77 peakContractPeriodYear is ' +tmpPeakContractPeriodYear);

```

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [December 20, 2022, 3:32pm UTC](https://community.homey.app/t/js-partly-reduce-two-arrays/73674/8 "2022-12-20T15:32:00Z")

</div>

Think i have the solution. Added some variables with the day, month and year when it is yesterday and the day, month and year when it is the previous month.

```auto
var date = new Date();
date.setDate(date.getDate());
const currentDay = date.getDate();
console.log('current day is: ' +currentDay)

date.setDate(date.getDate() -(+currentDay));
const lastDayPreviousMonth = date.getDate();
console.log('lastDayPreviousMonth is: '+lastDayPreviousMonth)
const previousMonth = date.getMonth(); // getMonth() returns month from 0 to 11
console.log('previousMonth is: '+previousMonth)
const yearPreviousMonth = date.getFullYear();
console.log('yearPreviousMonth is: '+yearPreviousMonth)

var date = new Date();
date.setDate(date.getDate());
console.log('current day is: ' +currentDay)
const currentMonth = date.getMonth(); // getMonth() returns month from 0 to 11
console.log('current month is: '+(currentMonth +1))
const currentYear = date.getFullYear();
console.log('current year is: '+currentYear)

function getDaysInMonth(year, month) {
  return new Date(year, month, 0).getDate();

}
const daysInCurrentMonth = getDaysInMonth(currentYear, currentMonth +1);
console.log('11 daysInCurrentMonth is: '+daysInCurrentMonth)

var tmpPeakDay = global.get('peakDay' +currentYear +(currentMonth +1));
tmpPeakDay.splice(currentDay -1,1,peak);
global.set('peakDay'+currentYear +(currentMonth +1), tmpPeakDay);
console.log('67 peakDay'+currentYear +(currentMonth +1) +' is ' +tmpPeakDay);
var peakDay = tmpPeakDay[currentDay - 1];
console.log('68 peakDay is ' +peakDay);
var tmpPeakPreviousDay = global.get('peakDay' +yearMinusOneDay +(monthMinusOneDay +1));
console.log('69 tmpPeakPreviousDay' +yearMinusOneDay +(monthMinusOneDay +1) +' is ' +tmpPeakPreviousDay);
var peakPreviousDay = tmpPeakPreviousDay[dayMinusOneDay -1];
console.log('70 peakPreviousDay day '+[dayMinusOneDay -1] +' is: '+peakPreviousDay);
var peakDayResult = peakDay -peakPreviousDay
console.log('71 peakDayResult is: ' +peakDay +' - ' +peakPreviousDay +' is ' +peakDayResult);

var tmpPeakMonth = global.get('peakMonth' +currentYear);
console.log('72 tmpPeakMonth'+currentYear +' is: ' +tmpPeakMonth);
var tmpPeakPreviousMonth = global.get('peakMonth' +yearPreviousMonth);
console.log('73 tmpPeakPreviousMonth' +yearPreviousMonth +' is: ' +tmpPeakPreviousMonth);
tmpPeakMonth.splice(currentMonth,1,peakDay - lastDayPreviousMonth);
global.set('peakMonth'+currentYear, tmpPeakMonth);
console.log('74 peakMonth' +currentYear +' is ' +tmpPeakMonth);

var energyContractMonth = '8';

var energyContractYear = global.get('energyContractYear');
console.log('77 energyContractYear is: '+energyContractYear);
if(energyContractYear === currentYear)
{
var tmpPeakContractPeriodYear = global.get('peakContractPeriodYear');
console.log('78 tmpPeakContractPeriodYear is: '+tmpPeakContractPeriodYear);
var tmpPeakContractPeriodMonth = global.get('peakMonth' +currentYear);
console.log('79 tmpPeakContractPeriodMonth' +currentYear +' is: '+tmpPeakContractPeriodMonth);
var tmp = tmpPeakContractPeriodMonth.slice(energyContractMonth -1,tmpPeakContractPeriodMonth.length);
console.log('80 tmpPeakContractPeriodMonth' +currentYear +' is: '+tmp);
global.set('peakContractPeriodMonth' +currentYear, tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
tmpPeakContractPeriodYear.splice(currentYear -2001,1,tmp1);
console.log('81 tmpPeakContractPeriodYear is: '+tmpPeakContractPeriodYear);
global.set('peakContractPeriodYear', tmpPeakContractPeriodYear);
}
{
var tmpPeakContractPeriodYear = global.get('peakContractPeriodYear')
var tmpPeakContractPeriodMonth = global.get('peakContractPeriodMonth' +(currentYear -1));
var tmp = tmpPeakContractPeriodMonth.slice(0,energyContractMonth -2);
global.set('peakContractPeriodMonth' +(currentYear -1), tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
var tmp2 = tmpPeakContractPeriodYear.slice((currentYear -1) -2001,tmp1);
global.set('peakContractPeriodYear', tmp2);
}

if(currentMonth -1 === energyContractMonth - 2)
{
global.set('energyContractYear', +currentYear)
}

Hope this will work

```
