简体   繁体   中英

How to loop through enum values by a specific value in Javascript

I'm fairly new to Javascript and using enums in general and I can't seem to find a way to loop through an enum the way I'd like to.

I have the following enum:

export var EnumVar = 
{
    ENTRY0:         0,
    ENTRY1:         1,
    ENTRY2:         2,
    ENTRY3:         3,
    ENTRY4:         4,
    ENTRY5:         5,
    ENTRY6:         6,
    ENTRY7:         7,
    ENTRY8:         8,
    ENTRY9:         9,

    properties:
    {
        0: {name: "Entry Number One"},
        1: {name: "Entry Number Two"},
        2: {name: "Entry Number Three"},
        3: {name: "Entry Number Four"},
        4: {name: "Entry Number Five"},
        5: {name: "Entry Number Six"},
        6: {name: "Entry Number Seven"},
        7: {name: "Entry Number Eight"},
        8: {name: "Entry Number Nine"},
        9: {name: "Entry Number Ten"}
    }
}

I know that if I use console.log(EnumVar.ENTRY0); then it will print out 0 . I know that I can use:

for (element in EnumVar.properties)
{
    console.log(element.toString());
}

and it will (seemingly) randomly print out the numbers.

I have an enum list that extremely large with 100+ entries that I want to loop through. I don't need to loop through every single entry but could do every 10 - 20, for example. I can't seem to find a way to iterate like element in where I can tell it to iterate by a unit of say 10 or 20. Can anyone help me find a resource to figure this out?

Two things, first of all "technically" you can't use numbers as object keys (you can but as you noted the order is random depending on the browser implementation).

Second, if you want to do every 10th entry, use Object.keys. Ie

var keys = Object.keys(EnumVar.properties);
for (var i = 0; i < keys.length; i += 10) {
  console.log(i.toString() + ' contains ' + JSON.stringify(EnumVar.properties[i]));
}

Note: Object.keys will produce a random order just as for in will, if you actually have a number as the key, it is best to an array -- but there is no short notation :

var tmp = {1: 'test-1', 10: 'test-10'}  

for sparse arrays like objects -- you will have to iniate them one by one.

var tmp = []; 
tmp[1] = 'test-1';
tmp[10] = 'test-10';

You can create an array of object keys using Object.keys and then aplit array for chunks using slice

let array = Object.keys(EnumVar.properties)
let i,j,temparray,chunk = 10;
for (i=0,j=array.length; i<j; i+=chunk) {
    temparray = array.slice(i,i+chunk);
    // do whatever
    for (element in temparray)
    {
        console.log(element.toString(), EnumVar.properties[element] );
    }
}

You just need to get the enum value, then use it to access the property. Check out the link for details https://stijndewitt.com/2014/01/26/enums-in-javascript/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM