简体   繁体   中英

Javascript switch statement is giving me an error. what is going on?

I'm not all that used to using switch statements. They don't seem like a complex thing and their syntax is pretty straight forward. I have the most basic of basic switch statements here, but it is returning an error for some reason. I cannot for the life of me figure it out.

Here is the switch statement:

switch(props.results.pvp1) {
            case 3:
                classes.filter((class) => {
                    console.log(class)
                })
                break;
            default:
                break;
        }

The error that is showing up is:

Line 147:38:  Parsing error: Unexpected token, expected "{"

  145 |         switch(props.results.pvp1) {
  146 |             case 3:
> 147 |                 classes.filter((class) => {
      |                                      ^
  148 |                     console.log(class)
  149 |                 })
  150 |                 break;

Any ideas? Thank you in advance!

Use this instead:

switch(props.results.pvp1) {
    case 3:
        classes.forEach((item) => {
            console.log(item)
        })
        break;
    default:
        break;
}

Note that filter method of arrays, execute an expression and if equals to true push that item into an array that returned by function, So you must use forEach method instead, that execute a function on all items of array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM