How to get an array of all enum values in C#?
This gets you a plain array of the enum values using Enum.GetValues
:
var valuesAsArray = Enum.GetValues(typeof(Enumnum));
And this gets you a generic list:
var valuesAsList = Enum.GetValues(typeof(Enumnum)).Cast<Enumnum>().ToList();
How to get all enum values as an array
For Swift 4.2 (Xcode 10) and later
There's a CaseIterable
protocol:
enum EstimateItemStatus: String, CaseIterable {
case pending = "Pending"
case onHold = "OnHold"
case done = "Done"
init?(id : Int) {
switch id {
case 1: self = .pending
case 2: self = .onHold
case 3: self = .done
default: return nil
}
}
}
for value in EstimateItemStatus.allCases {
print(value)
}
For Swift < 4.2
No, you can't query an enum
for what values it contains. See this article. You have to define an array that list all the values you have. Also check out Frank Valbuena's solution in "How to get all enum values as an array".
enum EstimateItemStatus: String {
case Pending = "Pending"
case OnHold = "OnHold"
case Done = "Done"
static let allValues = [Pending, OnHold, Done]
init?(id : Int) {
switch id {
case 1:
self = .Pending
case 2:
self = .OnHold
case 3:
self = .Done
default:
return nil
}
}
}
for value in EstimateItemStatus.allValues {
print(value)
}
How to get all the values of an enum with typescript?]
You have to filter out the numeric keys, either via Object.values
or Object.keys
:
const colors = Object.keys(Color).filter((item) => {
return isNaN(Number(item));
});
console.log(colors.join("\n"));
This will print:
Red
Green
Blue
A TypeScript enum will transpile in the end into a plain JavaScript object:
{
'0': 'Red',
'1': 'Green',
'2': 'Blue',
Red: 0,
Green: 1,
Blue: 2
}
So you can use the numeric index as key to get the value, and you can use the value to lookup its index in the enum:
console.log(Color[0]); // "Red"
console.log(Color["0"]); // "Red"
console.log(Color["Red"]) // 0
How to get all values of an enum in PHP?
After some research I found the answer. You can use the static method: cases()
.
enum Status
{
case PAID;
case Cancelled;
}
Status::cases();
The cases method will return an array with an enum (UnitEnum
interface) for each value.
Typescript enum values as array
Yes, it is possible to use:
Object.values(MyEnum)
because enum is an JS object after compilation:
var MyEnum;
(function (MyEnum) {
MyEnum["FOO"] = "foo";
MyEnum["BAR"] = "bar";
})(MyEnum || (MyEnum = {}));
Get All Enum Values To A List
You need to map
with getValue
List<String> fruits = Stream.of(FruitsEnum.values())
.map(FruitsEnum::getValue) // map using 'getValue'
.collect(Collectors.toList());
System.out.println(fruits);
this will give you the output
[APPL, BNN]
How to get all values from python enum class?
You can use IntEnum:
from enum import IntEnum
class Color(IntEnum):
RED = 1
BLUE = 2
print(int(Color.RED)) # prints 1
To get list of the ints:
enum_list = list(map(int, Color))
print(enum_list) # prints [1, 2]
Converting enums to array of values (Putting all JSON values in an array)
I would convert the map into an array and store it as types.all
.
You can create a method that does it automatically:
function makeEnum(enumObject){
var all = [];
for(var key in enumObject){
all.push(enumObject[key]);
}
enumObject.all = all;
}
Related Topics
How to Count Rows in a Parse Class Programmatically in an iOS App Using Swift
Custom Tabbar Layout for Uitabbarviewcontroller
Alamofire:How to Handle Errors Globally
Uiimageview Aspect Fit and Center
Afnetworking: Handle Error Globally and Repeat Request
Xcode - Error Itms-90635 - Invalid MACh-O in Bundle - Submitting to App Store
Popping and Pushing View Controllers in Same Action
Using Enum as Property of Realm Model
iOS 9 Searchbar Disappears from Table Header View When Uisearchcontroller Is Active
How to Use Multi-Path Update with the Rest API in Firebase? Error 400
Pass Variables to a New View Controller via a Subclass and Dozens of Map Pins
Disable the Interactive Dismissal of Presented View Controller
Change the Font of a Uibarbuttonitem
How to Get Distance of Object from iPhone Camera Using Image Exif Meta Data
Programmatically Linking Uipagecontrol to Uiscrollview