1

I am working with a multi-dimensional array like below

[
    [203, 541, 5001, 6.8, 207, 30252], 
    [203, 542, 5001, 16.3, 83, 50832], 
    [203, 542, 5001, 60.9, 207, 30252],
    [203, 542, 5003, null, 207, 30252],
    [203, 541, 5001, 15, 965, 52452],
    [203, 542, 5003, 6.8,207, 30252],
    [203, 542, 5003, null, 207, 30252],
    [203, 541, 5001, 15, 965, 52452],
    [203, 542, 5003, 6.8,207, 30252]
]

Where the first 3 elements are ID ints, and the last 3 are count floats. I am trying to be able to group these based on the first 3 elements and add the counts (individually) so I would end with an array like below

[
    [203, 541, 5001, 36.8, 1937, 30252], // Sum the last 3 elements that have 203,541,5001 as the beginning 3 elements of the array
    [203, 542, 5001, 77.2, 290, 81084], // Sum the last 3 elements that have 203,541, 5001 as the beginning 3 elements of the array
    [203, 541, 5003, a, b, c], // same as above
    [203, 542, 5003, x, y, z] // same as above
]

Without looping through all arrays, creating temp counters, then looping through each array element to add the last 3 elements to the temp counters and then pushing the result to a result array, is there a way to group these and add at the same time?

2
  • what code have you got so far? Commented Apr 8, 2021 at 21:41
  • essentially looping through the array, creating temp counters (as the first 3 elements), looping through each inner array, and adding to the temp counters, was wondering if there is a better way to do this Commented Apr 8, 2021 at 22:13

1 Answer 1

2

I assume that you tried any code implementation to solve your problem. Anyway here is my answer...

To solve your problem you need to do at last 2 operation, group and modify.

const data = [
    [203, 541, 5001, 6.8, 207, 30252],
    [203, 542, 5001, 16.3, 83, 50832],
    [203, 542, 5001, 60.9, 207, 30252],
    [203, 542, 5003, null, 207, 30252],
    [203, 541, 5001, 15, 965, 52452],
    [203, 542, 5003, 6.8, 207, 30252],
    [203, 542, 5003, null, 207, 30252],
    [203, 541, 5001, 15, 965, 52452],
    [203, 542, 5003, 6.8, 207, 30252]
];
function group(data) {
    let group = new Map;
    let result = [];
    for (let entry of data) {
        // You can comment this line, If you don't need `data`.
        entry = [...entry]; // Cloning, Don't want to modify `data` directly...
        const key = entry.splice(0, 3).join(',');
        const item = group.get(key);
        if (!item) group.set(key, entry)
        else for (let i = 0; i < entry.length; i++) {
            item[i] += entry[i];
        }
    }
    for (const [key, value] of group) {
        result.push(key.split(',').concat(value));
    }
    return result;
}
console.log(group(data));

Result:

[
    ["203", "541", "5001", 36.8, 2137, 135156],
    ["203", "542", "5001", 77.2, 290, 81084],
    ["203", "542", "5003", 13.6, 828, 121008],
]

Currently, type of group key is string, You can convert it to number, If you want, But I didn't...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I essentially did the same thing, however, I didn't originally use the Map object, but changed to use that for making the getting and setting more efficient. Thanks for the help

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.