Home > database >  return value 0 instead of undefined
return value 0 instead of undefined

Time:07-15

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.

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?

CodePudding user response:

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));

CodePudding user response:

If you want to handle undefined elements in one line, you can try something like:

console.log ("\t\tplatinum: "   (subMemTypeCount["Platinum"])?subMemTypeCount["Platinum"]:0);

This is basically checking if it is defined or not, If defined -> prints, else 0

  • Related