enumeration javascript code example
Example 1: javascript enumerate
const foobar = ['A', 'B', 'C'];
for (const [index, element] of foobar.entries()) {
console.log(index, element);
}
Example 2: javascript enum
const seasons = {
SUMMER: {
BEGINNING: "summer.beginning",
ENDING: "summer.ending"
},
WINTER: "winter",
SPRING: "spring",
AUTUMN: "autumn"
};
let season = seasons.SUMMER.BEGINNING;
if (!season) {
throw new Error("Season is not defined");
}
switch (season) {
case seasons.SUMMER.BEGINNING:
case seasons.SUMMER.ENDING:
case seasons.SUMMER:
case seasons.WINTER:
case seasons.SPRING:
case seasons.AUTUMN:
}
Example 3: enum javascript
const DaysEnum = {"monday":1, "tuesday":2, "wednesday":3, ...}
Object.freeze(DaysEnum)
Example 4: javascript enumerators
array.every(elm,index,array)
array.find(elm,index,array)
array.findIndex(elm,index,array)
array.map(elm,index,array)
array.reduce(accumulator,elm,index,array)
array.some(elm,index,array)