-1

i create the below posted color enumeration in a separate file of .ts extension. i would like to know how to use or to call the enumeration in the for-loop in the posted code in such way that to be able to iterate throw it. in other words, how to iterate through the enumeration posted below in a loop so that for ì = 0 RED is set and when i=7 LIMEis set.

the double question mart, as apparent, is to be replaced be the iteration through the enumeration.

code:

for(let i = 0; i < centerPointsClusters.length;i++) {
            this.centerPoint = this.APIService.visualisePoint(this.map,centerPointsClusters[i], ??)
        }
        

enum:

export enum ColorEnum {
    RED = "#F44336",
    PINK = "#FF4081",
    PURPLE = "#9C27B0",
    INDIGO = "#536DFE",
    BLUE = "#2196F3",
    TEAL = "#64FFDA",
    GREEN = "#4CAF50",
    LIME = "#EEFF41",
    YELLOW = "#FFEB3B",
    ORANGE = "#FFAB40"
  }
  
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • @R.Richards no it does not. i want to iterate through the enumeration according to an index of i in the loop – Amrmsmb Jun 07 '21 at 11:47

2 Answers2

0

AFAIK, enum in typescript is basically an object. So you could take the keys and iterate through it

import { ColorEnum } from './ColorEnum';
const enumKeys = Object.keys(ColorEnum);

for(let i = 0; i < centerPointsClusters.length;i++) {
    this.centerPoint = this.APIService.visualisePoint(this.map, centerPointsClusters[i], enumKeys[i])
}
Pratansyah
  • 457
  • 2
  • 14
0

Maybe you can use an obj instead of enum

const obj =  {
RED : "#F44336",
PINK : "#FF4081",
PURPLE : "#9C27B0",
INDIGO : "#536DFE",
BLUE : "#2196F3",
TEAL : "#64FFDA",
GREEN : "#4CAF50",
LIME : "#EEFF41",
YELLOW : "#FFEB3B",
ORANGE : "#FFAB40"
};


const iterateObj = () => {
for(let i in obj) {
    console.log(i, obj[i]);
  }
}
Bhaskar Joshi
  • 413
  • 7
  • 18