Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.each on Enum JQuery

Tags:

jquery

enums

each

I have an Enum defined in my Javascript like below:

BankTypesEnum = {
        'Savings': '0',
        'HomeLoan': '1',
        'Current': '2',
       'salary': '3'
    }

I want to run $.each() on this and compare with values from some other data source. Can anybody help me in this?

like image 914
guravman Avatar asked Sep 21 '12 06:09

guravman


People also ask

Can you forEach an enum?

Enums don't have methods for iteration, like forEach() or iterator(). Instead, we can use the array of the Enum values returned by the values() method.

Can you iterate through enums?

To iterate through an enumeration, you can move it into an array using the GetValues method. You could also iterate through an enumeration using a For... Each statement, using the GetNames or GetValues method to extract the string or numeric value.

Can I iterate through enum typescript?

Iterating over the values In order to iterate over the values of this enum, we can use the Object. values() built-in function, which returns an array whose elements are the enumerable property values found on the object.


2 Answers

$.each(BankTypesEnum , function(key, value) { 
  alert(key + ': ' + value); 
});

edit this code with your needs, instead of alert put compare or anything else you need to do with keys and values

like image 176
divide by zero Avatar answered Oct 20 '22 22:10

divide by zero


Is it necessary for you to use $.each()? If looping through them is the purpose, you can simply use for for in loop like this:

var earnings=BankTypesEnum;
for(var myEarnings in earnings)
{
    //do something here
}
like image 30
beNerd Avatar answered Oct 20 '22 22:10

beNerd