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,212
  • 1
  • 14
  • 27

2 Answers2

0
var colorKeys = Object.keys(colors);

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

Try it

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

for(let i in colors){
console.log(colors[i]);
}
abdulbarik
  • 6,101
  • 5
  • 38
  • 59