0

Is there a way to loop over the following variable and print out it's values (0,1,2) i've tried to run forEach on it, but i get an error that forEach is not a function.

let colors = {
RED: 0,
GREEN: 1,
BLUE: 2 };
Yoni Mayer
  • 1,118
  • 1
  • 12
  • 26

2 Answers2

0
var colorKeys = Object.keys(colors);

for(int i = 0; i < colorKeys.length; i++){
    console.log(colors[colorKeys[i]]);
}
ardilgulez
  • 1,746
  • 17
  • 19
0

Try it

let colors = {
RED: 0,
GREEN: 1,
BLUE: 2 }; 

for(let i in colors){
console.log(colors[i]);
}
abdulbarik
  • 5,757
  • 4
  • 33
  • 57