如何在 switch 语句中使用数组值作为 case

问题描述 投票:0回答:7

我有一个值数组:

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

我想在 switch 语句中使用每个值作为一个 case:

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');
}

数组中的值是自动生成的,因此它不是固定大小的数组。我无法在括号之间提出表达式。这应该用 forEach 来完成吗?我应该首先拆分值吗?但那又怎样呢?将它们存储在单独的变量中?

javascript arrays switch-statement
7个回答
1
投票

将开关置于函数中。使用 forEach 或带有索引的 for 循环调用该函数。

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]);
}


0
投票
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;
    }
}

0
投票

在 switch 语句中一次只能使用一个值,因为 switch 语句旨在将一个值与多个选项进行比较,这就是我们使用它的原因。

如果您想对数组中的所有值使用 switch 语句,那么您需要循环数组。

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

0
投票

您可以尝试用布尔值(例如true)来评估

const arr = ['first', 'second', 'third', ...];

switch (true){
 case arr.includes('first'):
  console.log('first');
  break;
 case arr.includes('second'):
  console.log('second');
  break;
  ...
 default:
  console.log('nothing')
}

-1
投票
const array = ['first', 'second', 'third', 'fourth'];

创建一个函数,在函数内运行 foreach 并在循环内运行 switch

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');
    }
  });

}

然后当然使用数组作为参数来运行你的函数

looparray(array);

-2
投票

那它是什么?

switch (array[0]) {

这是一个什么样的故事,马克?


-3
投票

您在寻找这样的东西吗?

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');
}
© www.soinside.com 2019 - 2024. All rights reserved.