Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

return value 0 instead of undefined

for (var i = 0; i < memberGroup.length; i++) {
  subMemberType.push(memberGroup[i]["membershipType"])
}

var subMemTypeCount = [];
while (true) {
  subMemberType.forEach(element => {
    subMemTypeCount[element] = (subMemTypeCount[element] || 0) + 1;
  });

  console.log("\t\truby: " + subMemTypeCount["Ruby"]);
  console.log("\t\tgold: " + subMemTypeCount["Gold"]);
  console.log("\t\tplatinum: " + subMemTypeCount["Platinum"]);
  console.log("\t\tdiamond: " + subMemTypeCount["Diamond"]);
  break;
}
Output:
                ruby: 2
                gold: 2
                platinum: undefined
                diamond: 1

What I am trying to achieve is to print out each membership type "ruby", "gold", "platinum", & "diamond".

I used a forEach to loop through the array subMemberType to count the number of duplicated membership type.

memberGroup[i]["membershipType"] is an 2D array, with a constructor membershipType.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My problem is that when I pushed memberGroup[i]["membershipType"] to array subMemberType, there wasn’t the "platinum" membership type. Hence, when I loop through the array using for each to find the duplicated membership type, it returns as undefined. However, I would like it to return "0" instead of "undefined". Is there anyway I can do that?

>Solution :

Instead of setting subMemTypeCount to an empty array (which should have been an object), set it to an object containing those for properties with the value 0. Then you can also reduce the body of the .forEach() to subMemTypeCount[element]++.

As an aside: a while-loop that loops unconditionally and breaks unconditionally after the first iteration, is fully redundant.

const subMemberType = ['Ruby', 'Diamond', 'Ruby', 'Gold', 'Gold'];

var subMemTypeCount = {'Ruby': 0, 'Gold': 0, 'Platinum': 0, 'Diamond': 0};
subMemberType.forEach(element => {
  subMemTypeCount[element]++;
});

console.log("\t\truby: " + subMemTypeCount["Ruby"]);
console.log("\t\tgold: " + subMemTypeCount["Gold"]);
console.log("\t\tplatinum: " + subMemTypeCount["Platinum"]);
console.log("\t\tdiamond: " + subMemTypeCount["Diamond"]);

Alternatively you can use the Nullish coalescing operator (??), or the Logical OR (||) in case the nullish coalescing operator isn’t supported.

const subMemberType = ['Ruby', 'Diamond', 'Ruby', 'Gold', 'Gold'];

var subMemTypeCount = {};
subMemberType.forEach(element => {
  subMemTypeCount[element] = (subMemTypeCount[element] || 0) + 1;
});

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading