For example
enum ABC { A = "a", B = "bb", C = "ccc" };
alert("B" in ABC); // true
alert("bb" in ABC); // false (i wanna true)
Please, keep in mind that we discuss about string enum features.
toList()). contains(aString); EventNames is the name of the enum while getEvent() is what returns the associated string value of each enum member.
Enum. IsDefined is a check used to determine whether the values exist in the enumeration before they are used in your code. This method returns a bool value, where a true indicates that the enumeration value is defined in this enumeration and false indicates that it is not. The Enum .
Since C# doesn't support enum with string value, in this blog post, we'll look at alternatives and examples that you can use in code to make your life easier. The most popular string enum alternatives are: Use a public static readonly string. Custom Enumeration Class.
In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.
Your enum:
enum ABC {
A = "a",
B = "bb",
C = "ccc"
};
Becomes this after compilation (at runtime):
var ABC = {
A: "a",
B: "bb",
C: "ccc"
};
Therefore you need to check if any of the values in ABC
is "bb"
. To do this, you can use Object.values():
Object.values(ABC).some(val => val === "bb"); // true
Object.values(ABC).some(val => val === "foo"); // false
Your code:
enum ABC {
A = "a",
B = "bb",
C = "ccc"
};
is compiled to the following JavaScript (see demo):
var ABC;
(function (ABC) {
ABC["A"] = "a";
ABC["B"] = "bb";
ABC["C"] = "ccc";
})(ABC || (ABC = {}));
This is why you are getting true
for "A" in ABC
, and false
for "bb" in ABC
. Instead, you need to look (i.e. loop) for values yourself; a short one liner could be this:
Object.keys(ABC).some(key => ABC[key] === "bb")
(or you could iterate over values directly using Object.values
if supported)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With