Umro
Umro

Reputation: 19

How can I access arrays through switch condition?

I was learning about switch condition and I was wondering if I could access an array through switch condition, I tried to do that but always returns the default state

let color = ['red','blue','green'];

switch(color){
    case '0':
        console.log('the color is red');
        break;
    case '1':
        console.log('the color is blue');
        break;
    case '2':
        console.log('the color is green');
        break;
    default:
        console.log('the color is not red or blue or green');
        break;
}

Upvotes: 1

Views: 68

Answers (4)

spcoder199
spcoder199

Reputation: 1

var list = ["red", "blue", "green"];
switch( list.indexOf("red") ){
     case 0:
          console.log("This is red");
          break;
     case 1:
          console.log("This is green");
          break;
     case 2:
          console.log("This is blue");
          break;
}

I think this might be what you want.

Upvotes: 0

nick zoum
nick zoum

Reputation: 7315

I think this is what you are looking for

let color = ['red','blue','green'];

for(var index in color) {
  printColor(index);
}

function printColor(index) {
  switch(index){
    case '0':
      console.log('the color is red');
      break;
    case '1':
      console.log('the color is blue');
      break;
    case '2':
      console.log('the color is green');
      break;
    default:
      console.log('the color is not red or blue or green');
      break;
  }
}

Using a for in loop, not to be confused with a for of loop, you can iterate through the indexes of the array and use the index in the switch statement.

Furthermore, if this is not for practice or exercise, then you should be storing the mapping of the indexing in an object.

let color = ['red', 'blue', 'green'];

let map = {
  0: function () {
    console.log('the color is red');
  },
  1: function () {
    console.log('the color is blue');
  },
  2: function () {
    console.log('the color is green');
  }
};

function onDefault() {
  console.log('the color is not red or blue or green');
}

for (var index in color) {
  (map[index] || onDefault)();
}

Though in your case you could easily just do

let color = ['red', 'blue', 'green'];

const defaultValue = 'not red or blue or green';

for (var index in color) {
  console.log(`the color is ${color[index] || defaultValue}`);
}

Upvotes: 2

Aprillion
Aprillion

Reputation: 22330

In general, there are 2 approaches to convert a color number to color name - either a mapping (Array or Object) or conditions (using if or switch statements or similar), e.g.:

// using a map from integers to color names
const colors = ['red','blue','green']
let colorNumber = 0
console.log(`Color for number ${colorNumber} is ${colors[colorNumber]}`)

colorNumber = 5
console.log(`Color for number ${colorNumber} is ${colors[colorNumber] || 'unknown'}`)

// using a switch
switch (colorNumber) {
  case 0: {
    console.log('Color for number 0 is red')
    break
  }
  case 1: {
    console.log('Color for number 1 is blue')
    break
  }
  case 2: {
    console.log('Color for number 2 is green')
    break
  }
  default: {
    console.log('Color for other numbers is unknown')
    break
  }
}

Upvotes: 2

pramesh
pramesh

Reputation: 1964

Switch is used to compare if the value equals particular case or not. In your case you are passing array inside switch but the cases are string. Since array and string do not match you're always getting default result. To fix this you should change color from array to string.

This will run

let color = '0'

switch(color){
    case '0':
        console.log('the color is red');
        break;
    case '1':
        console.log('the color is blue');
        break;
    case '2':
        console.log('the color is green');
        break;
    default:
        console.log('the color is not red or blue or green');
        break;
}

Your bug is that you're trying to check the equality of array and string.

Upvotes: 0

Related Questions