0

So I'm trying to copy these subarrays into another array and change them so their values are cumulative as I put them in. In order to make them cumulative, I am using the map function and modeling that based on the constant variable I made. In my attempts to copy them over into the other array, I have tried concat, push, and other methods without success.

JS

const cumulative = (cumu => val => cumu += val)(0);
var series2 = series.map(cumulative);

            series: [
                [55, 65, 76, 88, 44, 33, 54, 65, 7, 98, 12, 109],
                [52, 25, 26, 82, 24, 23, 34, 65, 47, 59, 12, 19],
                [57, 68, 77, 78, 44, 43, 74, 16, 71, 91, 11, 29]
            ]

This array below is just to demonstrate the desired effect I am attempting to do...

series2: [
    [55, 120, 196... etc...],
    [...],
    [...]
]
5
  • I guess you have got a syntax error of parentheses there? Commented Dec 12, 2020 at 22:00
  • can you please post the contents of the second array? Commented Dec 12, 2020 at 22:00
  • The second array is just an example array, it is not created. Commented Dec 12, 2020 at 22:01
  • @PraveenKumarPurushothaman No, I do not get syntax errors. One attempt I tried ended up combining all three arrays into one and then pushing them into the second array, but that is not what I want. Commented Dec 12, 2020 at 22:02
  • @grahamfk45c I fixed it. Check out my answer. Commented Dec 12, 2020 at 22:05

3 Answers 3

1

Guess I cracked it. Your logic is wrong there, you should use an inner map:

series = [
  [55, 65, 76, 88, 44, 33, 54, 65, 7, 98, 12, 109],
  [52, 25, 26, 82, 24, 23, 34, 65, 47, 59, 12, 19],
  [57, 68, 77, 78, 44, 43, 74, 16, 71, 91, 11, 29]
];
const cumulative = (cumu) => {
  var val = 0;
  return cumu.map(c => val += c);
};
var series2 = series.map(cumulative);
console.log(series2);

I get this as output and this is what you're upto:

[
  [55, 120, 196, 284, 328, 361, 415, 480, 487, 585, 597, 706],
  [758, 783, 809, 891, 915, 938, 972, 1037, 1084, 1143, 1155, 1174],
  [1231, 1299, 1376, 1454, 1498, 1541, 1615, 1631, 1702, 1793, 1804, 1833]
]
Sign up to request clarification or add additional context in comments.

Comments

1

I think reduce is the best tool to transform an array in the way you want - to one of cumulative sums. Use this to define the function that transforms a single array, then just map it over the outer array:

const series = [
    [55, 65, 76, 88, 44, 33, 54, 65, 7, 98, 12, 109],
    [52, 25, 26, 82, 24, 23, 34, 65, 47, 59, 12, 19],
    [57, 68, 77, 78, 44, 43, 74, 16, 71, 91, 11, 29]
];

const makeCumulative = arr => arr.reduce((cums, current) => {
  const subtotal = cums[cums.length - 1] || 0;
  return [...cums, subtotal + current];
}, []);

const series2 = series.map(makeCumulative);

console.log(series2);

Comments

1

Using reduce:

let series = [
    [55, 65, 76, 88, 44, 33, 54, 65, 7, 98, 12, 109],
    [52, 25, 26, 82, 24, 23, 34, 65, 47, 59, 12, 19],
    [57, 68, 77, 78, 44, 43, 74, 16, 71, 91, 11, 29],
];
console.log(series.map(
    (ar) => ar.reduce((acc, el) => [...acc, el + (acc[acc.length - 1] || 0)], [])
));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.