I have an array within an array with dates and an amount
var finances = [ ['jan', 25000], ['feb', 22000], ['mars', 21000] ]
I want to find out by how much the number changes each month. Then I want to find the difference between this change each month and add it up. I thought I had it but it is not working as I thought and I am really lost. I think I may be trying it completely the wrong way. Sorry if this is poorly worded.
I want to get the average of the amount the number changes by each month. At the moment I ma getting NAN.
var finances = [
['Jan-2010', 867884],
['Feb-2010', 984655],
['Mar-2010', 322013],
['Apr-2010', -69417],
['May-2010', 310503],
['Jun-2010', 522857],
['Jul-2010', 1033096],
['Aug-2010', 604885],
['Sep-2010', -216386],
['Oct-2010', 477532],
['Nov-2010', 893810],
['Dec-2010', -80353],
['Jan-2011', 779806],
['Feb-2011', -335203],
['Mar-2011', 697845],
['Apr-2011', 793163],
['May-2011', 485070],
['Jun-2011', 584122],
['Jul-2011', 62729],
['Aug-2011', 668179],
['Sep-2011', 899906],
['Oct-2011', 834719],
['Nov-2011', 132003],
['Dec-2011', 309978],
['Jan-2012', -755566],
['Feb-2012', 1170593],
['Mar-2012', 252788],
['Apr-2012', 1151518],
['May-2012', 817256],
['Jun-2012', 570757],
['Jul-2012', 506702],
['Aug-2012', -1022534],
['Sep-2012', 475062],
['Oct-2012', 779976],
['Nov-2012', 144175],
['Dec-2012', 542494],
['Jan-2013', 359333],
['Feb-2013', 321469],
['Mar-2013', 67780],
['Apr-2013', 471435],
['May-2013', 565603],
['Jun-2013', 872480],
['Jul-2013', 789480],
['Aug-2013', 999942],
['Sep-2013', -1196225],
['Oct-2013', 268997],
['Nov-2013', -687986],
['Dec-2013', 1150461],
['Jan-2014', 682458],
['Feb-2014', 617856],
['Mar-2014', 824098],
['Apr-2014', 581943],
['May-2014', 132864],
['Jun-2014', 448062],
['Jul-2014', 689161],
['Aug-2014', 800701],
['Sep-2014', 1166643],
['Oct-2014', 947333],
['Nov-2014', 578668],
['Dec-2014', 988505],
['Jan-2015', 1139715],
['Feb-2015', 1029471],
['Mar-2015', 687533],
['Apr-2015', -524626],
['May-2015', 158620],
['Jun-2015', 87795],
['Jul-2015', 423389],
['Aug-2015', 840723],
['Sep-2015', 568529],
['Oct-2015', 332067],
['Nov-2015', 989499],
['Dec-2015', 778237],
['Jan-2016', 650000],
['Feb-2016', -1100387],
['Mar-2016', -174946],
['Apr-2016', 757143],
['May-2016', 445709],
['Jun-2016', 712961],
['Jul-2016', -1163797],
['Aug-2016', 569899],
['Sep-2016', 768450],
['Oct-2016', 102685],
['Nov-2016', 795914],
['Dec-2016', 60988],
['Jan-2017', 138230],
['Feb-2017', 671099]
];
for (var i = 0; i < finances.length; i ) {
for (var j = 0; j < finances[i].length; j ) {
if (typeof finances[i][j] !== 'string') {
onlyNumbers.push(finances[i][j])
console.log(finances[i][j]);
amountTotal = amountTotal (finances[i][j]);
}
}
}
for (var k = 1; k < onlyNumbers.length; k ) {
monthlyChanges.push(onlyNumbers[k] - onlyNumbers[k - 1]);
}
for (var m = 1; m < monthlyChanges.length; m ) {
monthlyChangesArray.push(monthlyChanges[m] - monthlyChanges[m - 1]);
monthlyChangesAmount = monthlyChangesAmount (monthlyChangesArray[m]);
}
console.log(onlyNumbers);
console.log(amountTotal);
console.log(monthlyChanges);
console.log(monthlyChangesArray);
console.log(monthlyChangesAmount);
CodePudding user response:
Go through every item except the first, and find the difference between it and the previous item.
var finances = [
['Jan-2010', 867884],
['Feb-2010', 984655],
['Mar-2010', 322013],
['Apr-2010', -69417],
['May-2010', 310503],
['Jun-2010', 522857],
['Jul-2010', 1033096],
['Aug-2010', 604885],
['Sep-2010', -216386],
['Oct-2010', 477532],
['Nov-2010', 893810],
['Dec-2010', -80353],
['Jan-2011', 779806],
['Feb-2011', -335203],
['Mar-2011', 697845],
['Apr-2011', 793163],
['May-2011', 485070],
['Jun-2011', 584122],
['Jul-2011', 62729],
['Aug-2011', 668179],
['Sep-2011', 899906],
['Oct-2011', 834719],
['Nov-2011', 132003],
['Dec-2011', 309978],
['Jan-2012', -755566],
['Feb-2012', 1170593],
['Mar-2012', 252788],
['Apr-2012', 1151518],
['May-2012', 817256],
['Jun-2012', 570757],
['Jul-2012', 506702],
['Aug-2012', -1022534],
['Sep-2012', 475062],
['Oct-2012', 779976],
['Nov-2012', 144175],
['Dec-2012', 542494],
['Jan-2013', 359333],
['Feb-2013', 321469],
['Mar-2013', 67780],
['Apr-2013', 471435],
['May-2013', 565603],
['Jun-2013', 872480],
['Jul-2013', 789480],
['Aug-2013', 999942],
['Sep-2013', -1196225],
['Oct-2013', 268997],
['Nov-2013', -687986],
['Dec-2013', 1150461],
['Jan-2014', 682458],
['Feb-2014', 617856],
['Mar-2014', 824098],
['Apr-2014', 581943],
['May-2014', 132864],
['Jun-2014', 448062],
['Jul-2014', 689161],
['Aug-2014', 800701],
['Sep-2014', 1166643],
['Oct-2014', 947333],
['Nov-2014', 578668],
['Dec-2014', 988505],
['Jan-2015', 1139715],
['Feb-2015', 1029471],
['Mar-2015', 687533],
['Apr-2015', -524626],
['May-2015', 158620],
['Jun-2015', 87795],
['Jul-2015', 423389],
['Aug-2015', 840723],
['Sep-2015', 568529],
['Oct-2015', 332067],
['Nov-2015', 989499],
['Dec-2015', 778237],
['Jan-2016', 650000],
['Feb-2016', -1100387],
['Mar-2016', -174946],
['Apr-2016', 757143],
['May-2016', 445709],
['Jun-2016', 712961],
['Jul-2016', -1163797],
['Aug-2016', 569899],
['Sep-2016', 768450],
['Oct-2016', 102685],
['Nov-2016', 795914],
['Dec-2016', 60988],
['Jan-2017', 138230],
['Feb-2017', 671099]
];
function findChanges(data) {
const changes = [];
for (let i = 1; i < data.length; i ) {
const curr = data[i], prev = data[i-1];
changes.push([`(${prev[0]})_to_(${curr[0]})`, curr[1] - prev[1]])
}
return changes;
}
const changes = findChanges(finances);
const changesOfChanges = findChanges(changes);
document.getElementById("a").textContent = JSON.stringify(changes, null, 2)
document.getElementById("b").textContent = JSON.stringify(changesOfChanges, null, 2)
<h1>Changes</h1>
<pre id="a"></pre>
<h1>Changes Of Changes</h1>
<pre id="b"></pre>
CodePudding user response:
Here you can try to get array of monthly changes :
var finances = [
["Jan-2010", 867884],
["Feb-2010", 984655],
["Mar-2010", 322013],
["Apr-2010", -69417],
["May-2010", 310503],
["Jun-2010", 522857],
["Jul-2010", 1033096],
["Aug-2010", 604885],
["Sep-2010", -216386],
["Oct-2010", 477532],
["Nov-2010", 893810],
["Dec-2010", -80353],
["Jan-2011", 779806],
["Feb-2011", -335203],
["Mar-2011", 697845],
["Apr-2011", 793163],
["May-2011", 485070],
["Jun-2011", 584122],
["Jul-2011", 62729],
["Aug-2011", 668179],
["Sep-2011", 899906],
["Oct-2011", 834719],
["Nov-2011", 132003],
["Dec-2011", 309978],
["Jan-2012", -755566],
["Feb-2012", 1170593],
["Mar-2012", 252788],
["Apr-2012", 1151518],
["May-2012", 817256],
["Jun-2012", 570757],
["Jul-2012", 506702],
["Aug-2012", -1022534],
["Sep-2012", 475062],
["Oct-2012", 779976],
["Nov-2012", 144175],
["Dec-2012", 542494],
["Jan-2013", 359333],
["Feb-2013", 321469],
["Mar-2013", 67780],
["Apr-2013", 471435],
["May-2013", 565603],
["Jun-2013", 872480],
["Jul-2013", 789480],
["Aug-2013", 999942],
["Sep-2013", -1196225],
["Oct-2013", 268997],
["Nov-2013", -687986],
["Dec-2013", 1150461],
["Jan-2014", 682458],
["Feb-2014", 617856],
["Mar-2014", 824098],
["Apr-2014", 581943],
["May-2014", 132864],
["Jun-2014", 448062],
["Jul-2014", 689161],
["Aug-2014", 800701],
["Sep-2014", 1166643],
["Oct-2014", 947333],
["Nov-2014", 578668],
["Dec-2014", 988505],
["Jan-2015", 1139715],
["Feb-2015", 1029471],
["Mar-2015", 687533],
["Apr-2015", -524626],
["May-2015", 158620],
["Jun-2015", 87795],
["Jul-2015", 423389],
["Aug-2015", 840723],
["Sep-2015", 568529],
["Oct-2015", 332067],
["Nov-2015", 989499],
["Dec-2015", 778237],
["Jan-2016", 650000],
["Feb-2016", -1100387],
["Mar-2016", -174946],
["Apr-2016", 757143],
["May-2016", 445709],
["Jun-2016", 712961],
["Jul-2016", -1163797],
["Aug-2016", 569899],
["Sep-2016", 768450],
["Oct-2016", 102685],
["Nov-2016", 795914],
["Dec-2016", 60988],
["Jan-2017", 138230],
["Feb-2017", 671099],
];
let result = finances.reduce((prev, curr, index, arr) => {
arr[index 1]
? prev.push([
`${arr[index][0]} - ${arr[index 1][0]}`,
arr[index 1][1] - arr[index][1],
])
: 0;
return prev;
}, []);
console.log(result);