Convert Arrays inside Array to string and return each sum of values

Hi,

Yesterday I saw this closed Post:

It came to mind that i would like to do exactly this, but have an Array as Input which contains 3 Arrays and then return the sum of each of their values in a new array. So for example i have this array.

Array = [[1,1,1,2],[3,3,2,1],[1,2]]

And i would like to get this as response: [5,9,3]

the code in the closed Post used is this:
input.split(/[^|\d.]+/g).reduce((a, c) => a + (Number(c) ?? 0), 0)

Even with the help of ChatGPT I could not get an correct Javascript Function to make this work.

In this Screenshot you can see my array “alleArray”, which contains 3 Arrays with Numbers returned as Strings from an API Call.
input

Here is the custom JS Module which I use for making a javascript completion.
module

Best regards!

You can’t use the split function in the JS code, because your array is already an array.

If you need further assistance, please provide the following:

Please provide the output bundles of the modules by running the scenario, then click the white speech bubble on the top-right of each module and select “Download output bundles”.
Screenshot_2023-10-06_141025

A.

Save the bundle contents in your text editor as a bundle.txt file, and upload it here into this discussion thread.
Uploading it here will look like this:

bundle.txt (12.3 KB)

B.

If you are unable to upload files on this forum, alternatively you can paste the formatted output bundle in this manner:

  • Either add three backticks ``` before and after the code, like this:

    ```
    input/output bundle content goes here
    ```

  • Or use the format code button in the editor:
    Screenshot_2023-10-02_191027

Providing the output bundles will allow others to replicate what is going on in the scenario even if they do not use the external service.

This will allow others to better assist you. Thanks!

2 Likes

@samliew

Thanks for trying to help!

Uploading the bundle.txt file does not work for me. So i copied it in there like you asked me to.

[
    {
        "alleArray": [
            [
                "9530000",
                "0",
                "0",
                "41080000",
                "5840000",
                "7210000",
                "0",
                "8960000",
                "0",
                "5760000",
                "17610000",
                "41680000",
                "0",
                "18630000",
                "1190000",
                "2950000",
                "0",
                "0",
                "12150000",
                "1830000",
                "5400000",
                "20590000",
                "0",
                "3180000",
                "1890000",
                "0",
                "2420000",
                "0",
                "0",
                "0",
                "5160000",
                "30400000",
                "0",
                "0",
                "0",
                "0",
                "2740000",
                "0",
                "9530000",
                "2070000",
                "0",
                "7680000",
                "15770000",
                "3850000",
                "27740000",
                "23380000",
                "15660000",
                "2060000",
                "2690000",
                "0",
                "0",
                "3840000",
                "6500000",
                "0",
                "6550000",
                "0",
                "3830000",
                "2010000",
                "58650000",
                "22210000",
                "9920000",
                "0",
                "19920000",
                "0",
                "29590000",
                "0",
                "1740000",
                "10400000",
                "1950000",
                "0",
                "2380000",
                "0",
                "0",
                "0",
                "0",
                "2350000",
                "0",
                "2990000",
                "1400000",
                "4950000",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "980000",
                "2150000",
                "2380000",
                "13410000",
                "0",
                "1780000",
                "0",
                "7320000",
                "14010000",
                "0",
                "5990000",
                "12240000",
                "7400000",
                "1540000",
                "1980000",
                "2730000",
                "0",
                "0",
                "10490000",
                "0",
                "5920000",
                "0",
                "4280000",
                "0",
                "42500000",
                "3560000",
                "1400000",
                "39330000",
                "1880000",
                "4130000",
                "1590000",
                "0",
                "1560000",
                "1280000",
                "2820000",
                "0",
                "0",
                "0",
                "0",
                "0",
                "24960000",
                "0",
                "0",
                "4700000",
                "0",
                "7990000",
                "3030000",
                "0",
                "0",
                "0",
                "0",
                "13820000",
                "0",
                "33990000",
                "1340000",
                "2930000",
                "0",
                "1890000",
                "0",
                "0",
                "7530000",
                "12410000",
                "134950000",
                "0",
                "0",
                "1920000",
                "4180000",
                "0",
                "6460000",
                "66650000",
                "2000000",
                "2210000",
                "0",
                "0",
                "0",
                "0",
                "5680000",
                "7720000",
                "2750000",
                "49240000",
                "4800000",
                "0",
                "0",
                "2610000",
                "0",
                "6380000",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "3680000",
                "0",
                "0",
                "0",
                "0",
                "0",
                "23100000"
            ],
            [
                "3",
                "0",
                "0",
                "10",
                "2",
                "2",
                "0",
                "2",
                "0",
                "2",
                "3",
                "8",
                "0",
                "8",
                "2",
                "10",
                "0",
                "0",
                "4",
                "1",
                "1",
                "4",
                "0",
                "1",
                "1",
                "0",
                "1",
                "0",
                "0",
                "0",
                "2",
                "8",
                "0",
                "0",
                "0",
                "0",
                "1",
                "0",
                "3",
                "1",
                "0",
                "2",
                "5",
                "1",
                "6",
                "6",
                "6",
                "1",
                "1",
                "0",
                "0",
                "1",
                "3",
                "0",
                "2",
                "0",
                "1",
                "1",
                "18",
                "10",
                "4",
                "0",
                "5",
                "0",
                "13",
                "0",
                "1",
                "3",
                "1",
                "0",
                "2",
                "0",
                "0",
                "0",
                "0",
                "2",
                "0",
                "3",
                "1",
                "2",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "1",
                "1",
                "1",
                "6",
                "0",
                "1",
                "0",
                "2",
                "4",
                "0",
                "3",
                "5",
                "2",
                "1",
                "1",
                "1",
                "0",
                "0",
                "3",
                "0",
                "3",
                "0",
                "2",
                "0",
                "10",
                "2",
                "1",
                "16",
                "1",
                "1",
                "1",
                "0",
                "1",
                "1",
                "1",
                "0",
                "0",
                "0",
                "0",
                "0",
                "8",
                "0",
                "0",
                "2",
                "0",
                "2",
                "1",
                "0",
                "0",
                "0",
                "0",
                "4",
                "0",
                "14",
                "1",
                "2",
                "0",
                "1",
                "0",
                "0",
                "3",
                "7",
                "35",
                "0",
                "0",
                "1",
                "1",
                "0",
                "3",
                "64",
                "2",
                "1",
                "0",
                "0",
                "0",
                "0",
                "2",
                "2",
                "1",
                "9",
                "1",
                "0",
                "0",
                "1",
                "0",
                "2",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "1",
                "0",
                "0",
                "0",
                "0",
                "0",
                "8"
            ],
            [
                "29",
                "2",
                "11",
                "155",
                "7",
                "47",
                "1",
                "25",
                "4",
                "23",
                "54",
                "70",
                "3",
                "51",
                "4",
                "38",
                "1",
                "1",
                "34",
                "11",
                "41",
                "46",
                "13",
                "24",
                "6",
                "1",
                "6",
                "7",
                "1",
                "2",
                "23",
                "44",
                "1",
                "1",
                "1",
                "3",
                "4",
                "9",
                "30",
                "3",
                "4",
                "6",
                "16",
                "13",
                "102",
                "85",
                "69",
                "10",
                "10",
                "2",
                "1",
                "9",
                "5",
                "3",
                "7",
                "2",
                "8",
                "70",
                "241",
                "287",
                "120",
                "38",
                "74",
                "1",
                "323",
                "6",
                "8",
                "56",
                "20",
                "2",
                "5",
                "1",
                "2",
                "1",
                "14",
                "11",
                "2",
                "8",
                "2",
                "13",
                "1",
                "6",
                "16",
                "1",
                "1",
                "1",
                "6",
                "1",
                "1",
                "2",
                "7",
                "7",
                "3",
                "7",
                "22",
                "63",
                "18",
                "15",
                "2",
                "33",
                "42",
                "56",
                "30",
                "32",
                "18",
                "1",
                "6",
                "10",
                "1",
                "6",
                "14",
                "11",
                "45",
                "26",
                "19",
                "16",
                "183",
                "16",
                "14",
                "147",
                "7",
                "6",
                "9",
                "9",
                "2",
                "7",
                "9",
                "8",
                "6",
                "3",
                "8",
                "3",
                "103",
                "2",
                "36",
                "73",
                "13",
                "26",
                "9",
                "2",
                "1",
                "1",
                "2",
                "49",
                "7",
                "178",
                "47",
                "14",
                "3",
                "19",
                "1",
                "18",
                "25",
                "84",
                "464",
                "1",
                "3",
                "12",
                "14",
                "3",
                "28",
                "445",
                "20",
                "3",
                "12",
                "6",
                "1",
                "9",
                "17",
                "18",
                "26",
                "65",
                "62",
                "4",
                "2",
                "36",
                "2",
                "11",
                "1",
                "2",
                "6",
                "2",
                "6",
                "1",
                "1",
                "12",
                "3",
                "5",
                "1",
                "12",
                "92"
            ]
        ]
    }
]

This is probably easier for you to understand,

  1. Iterate the array containing three arrays
  2. Iterate each value within each sub-array
  3. Sum items using Numeric Aggregator
  4. Get an array of results from the three arrays

Total of 8 operations.

2 Likes

@samliew

This is great Input! Problem with these arrays is, that numeric aggregator will not work, because of the arrays getting strings as result of the api calls before.

So i unfortunately cannot use them here. Thats why i came to using custom js in the first place.

Cheers

It would, I’ve even ran it with the data you provided, which is arrays of numeric text.

This is due to type coercion, as the numeric aggregator expects numbers, and converts everything to numbers.

For more information, see https://www.make.com/en/help/mapping/type-coercion.html

2 Likes

I’ve done more digging with CustomJS to see why it didn’t work for you.

1.

CustomJS input field flattened and merged your three array of values into a single comma-separated string. This way you won’t know which values belong to which array, even if you split them!

So, the workaround is send each array into the module one array at a time, using an iterator.

2.

Next, the JS is not exactly correct.

It should be

input.split(', ').reduce((a, c) => +c+a, 0)

Output:

Once these issues are fixed, the scenario runs fine.

BUT!!!!!!!!!!!!

3.

The output of a CustomJS module, the result is a collection with variable Output, as seen in the screenshot.

When you array aggregate this, you don’t get a basic/primitive array.

Screenshot_2024-02-08_230240

So you might still need to spend one more Set Variable module to “convert” this.

See the difference in the output, compared to the bundle above?

Screenshot_2024-02-08_230207

4 Likes

@samliew

So many good Answers from you in a single Day!

I actually applied what you told me before and i was successful. So thank you very much, for taking the time and effort. I really appreciate it!

3 Likes