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?
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.
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.
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.
$.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
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
}
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