Home > database >  Identifying & Adding values in an Array
Identifying & Adding values in an Array

Time:07-15

class Member {
    constructor (membershipType, pointsEarned) {
        this.membershipType = membershipType;
        this.pointEarned = pointsEarned;
    }
}
var John = new Member ('Gold', 1400);
var Luke = new Member ('Ruby', 250);
var Sam = new Member ('Gold', 3350);
var Kelly = new Member ('Diamond', 40200);
Output:
       ruby: 250
       gold: 4750
       platinum: 0
       diamond: 40200

What I am trying to achieve is adding the "pointEarned" values that are tied to the same "membershipType" respectively to be added together.

For example if there are 2 gold "membershipType", I would like to add both 1400 & 3350 together. Is there any way I could do that?

I have used for loop to loop through the "membershipType". However, I can't wrap my head around it still. This is the code I have tried but it does not make sense.

for (var i = 0; i < memberGroup.length; i  ) {
                    if ("Ruby" == memberGroup[i]["membershipType"]) {
                        for (let x in memberGroup[i]) {
                        }
                    }
                }
                    console.log ("\t\truby: "   (subMemTypeCount["Ruby"] ?? 0));
                    console.log ("\t\tgold: "   (subMemTypeCount["Gold"] ?? 0));
                    console.log ("\t\tplatinum: "   (subMemTypeCount["Platinum"] ?? 0));
                    console.log ("\t\tdiamond: "   (subMemTypeCount["Diamond"] ?? 0));

CodePudding user response:

I'm going to try and explain this without using your original code, just to try and keep things minimal.

So lets's forget having a class, as it's extra noise in the code for now. We can just as easily define an object literal:

var John = { membershipType: 'Gold', pointsEarned: 1400 };

Without seeing your memberGroup, we can assume it's some sort of array, like:

const memberGroup = [
    John,
    Luke,
    Sam,
    Kelly,
];

Looking at your code, we can see that you're looking to get out the sums for each type. It'd be nice to store thoes types somewhere so we know what we're looking for:

const types = [
    'Gold',
    'Ruby',
    'Platinum',
    'Diamond'
];

Now we need to know how to get the sum for a given type:

const sumForType = (type, members) => members
    .reduce(
        (currentSum, member) => {
            if (member.membershipType === type) {
                return currentSum   member.pointsEarned;
            }

            return currentSum;
        }
    );

Reduce is a nice way of taking multiple things, and reducing them down to a single value.

So now, we can just get the sum for each type:

const subMemTypeCount = types.reduce(
    (sumsForTypes, type) => {
        sumForTypes[type] = sumForType(type, memberGroup);
        return sumsForTypes;
    },
    {},
);

So, putting it all together, we get something like this:

var John = { membershipType: 'Gold', pointsEarned: 1400 };
var Luke = { membershipType: 'Ruby', pointsEarned: 250 };
var Sam = { membershipType: 'Gold', pointsEarned: 3350 };
var Kelly = { membershipType: 'Diamond', pointsEarned: 40200 };

const memberGroup = [
    John,
    Luke,
    Sam,
    Kelly,
];

const types = [
    'Gold',
    'Ruby',
    'Platinum',
    'Diamond'
];

const sumForType = (type, members) => members
    .reduce(
        (currentSum, member) => {
            if (member.membershipType === type) {
                return currentSum   member.pointsEarned;
            }

            return currentSum;
        },
        0,
    );

const subMemTypeCount = types.reduce(
    (sumsForTypes, type) => {
        sumsForTypes[type] = sumForType(type, memberGroup);
        return sumsForTypes;
    },
    {},
);

console.log(subMemTypeCount);

Or, if it were me, I'd like to write it like this

  • Related