Home > Enterprise >  How to use array values as a case in a switch statement
How to use array values as a case in a switch statement

Time:09-10

I have an array of values:

const array = ['first', 'second', 'third', 'fourth'];

I want to use each of the values as a case in a switch statement:

switch (???) {
 case 'first':
  console.log('This is first');
  break;
 case 'second':
  console.log('This is second');
  break;
 case 'third':
  console.log('This is third');
  break;
 case 'fourth':
  console.log('This is fourth');
  break;
 default:
 console.log('None');
}

Values in an array are generated automatically, so it is not a fixed-size array. I can't come up with an expression in between parentheses. Should this be done somehow with a forEach? Should I first split values? But what then? Store them in a separate variable?

CodePudding user response:

const array = ['first', 'second', 'third', 'fourth'];
for (let i = 0; i < array.length; i  ) {
    switch (array[i]) {
        case 'first':
            console.log('This is first');
            break;
        case 'second':
            console.log('This is second');
            break;
        case 'third':
            console.log('This is third');
            break;
        case 'fourth':
            console.log('This is fourth');
            break;
        default:
            console.log('None');
            break;
    }
}

CodePudding user response:

Put the switch in a function. Call the function with a forEach or a for loop with the index.

function runCommand(command) {
  switch (command) {
    case 'first':
      console.log('This is first');
      break;
    case 'second':
      console.log('This is second');
      break;
    case 'third':
      console.log('This is third');
      break;
    case 'fourth':
      console.log('This is fourth');
      break;
    default:
      console.log('None');
  }
}

const array = ['first', 'second', 'third', 'fourth'];
array.forEach(runCommand);

for (let i=0; i<array.length;i   ){
  runCommand(array[i]);
}

CodePudding user response:

you can use only one value at a time in switch statement because switch statement is meant for comparing one value with multiple option that's why we use it.

if you want to use switch statement with all of values that are in array then you need to loop an array .

  array.forEach(currValue => {
      switch(currValue){
          case 'first':
      console.log('This is first');
      break;
      ...
      }
    })

CodePudding user response:

const array = ['first', 'second', 'third', 'fourth'];

Make a function, run your foreach inside the function and your switch inside your loop

function looparray(arr) {
  arr.forEach(function(value, index) {
    switch (value) {
      case 'first':
        console.log('This is first');
        break;
      case 'second':
        console.log('This is second');
        break;
      case 'third':
        console.log('This is third');
        break;
      case 'fourth':
        console.log('This is fourth');
        break;
      default:
        console.log('None');
    }
  });

}

Then of course run your function with your array as parameter

looparray(array);

CodePudding user response:

so what is it?

switch (array[0]) {

What a story, Mark?

CodePudding user response:

Are you looking for something like this?

switch (???) {
 case myArray[0]:
  console.log('This is first');
  break;
 case myArray[1]:
  console.log('This is second');
  break;
 case myArray[2]:
  console.log('This is third');
  break;
 case myArray[3]:
  console.log('This is fourth');
  break;
 default:
 console.log('None');
}
  • Related