1

I tend to use expression like this these days:

{ typeA: 'some code' }[type] ||
{ typeB: 'some other code' }[type] ||
defaultType

How do I fairly simple put multiple cases (something like:

switch (value)
{
   case typeA:
   case typeC:
      //do some stuff
      break;
   case typeB:
      //do some other stuff
   default:
       //default stuff
      break;
}

) in the code above? So it could work this way (invalid piece of code):

{ typeA || typeC: 'some code' }[type] ||
{ typeB: 'some other code' }[type] ||
defaultType

Backstory: I am utilising the code in React, so the 'some code' samples are actually React components.

user0101
  • 1,277
  • 1
  • 9
  • 17
  • 1
    Could you add information on what you are trying to acheive in the first statement of your question ? ( `{ typeA: 'some code' ...` ) – Nicolas Jan 20 '20 at 13:50
  • That should work because of fall through until it hits a break. Is the a specific error you're getting? See https://stackoverflow.com/questions/13207927/switch-statement-multiple-cases-in-javascript – Brett Gregson Jan 20 '20 at 13:50
  • Hey @BrettGregson. There's no error. I am just looking for an option to do multiple cases just like the 'switch' does (https://stackoverflow.com/a/13207939/12085047) – user0101 Jan 20 '20 at 13:58

3 Answers3

0

You're halfway there already. Just combine your objects into a big one:

    return ({
        typeA: "Foo",
        typeB: "Bar",
        get typeC() {//why not getters?
            return "computed value"
        }, 
    }[value]) || defaultValue;

I strongly recommend saving the case-object as a const or it'll re-allocate on every call.


This structure is suitable for values, but not conditional logic. If you need to execute different code on each case, you should either stick with switch, or make your object return functions:

const Values = {
    caseA: () => { /*...*/ },
    caseB: () => { /*...*/ },
   // ...etc
}


//get value from Values and execute function
Values['caseA']();

With this structure you don't get fall-through. If you want fall-through, stick with switch.

rath
  • 3,655
  • 1
  • 40
  • 53
  • Could you elaborate on 'I strongly recommend saving the case-object as a const or it'll re-allocate on every call.'? – user0101 Jan 20 '20 at 13:59
  • 1
    @user0101 Easier to show with an example. This is code I wrote many years ago: `function doSwitch(foo) { return {foo:"",bar:""}[foo]}`. Every time I invoke `doSwitch`, a new object is allocated. If I did it like that, the `vals` object will allocate once: `const vals = {/*...*/}; function doSwitch(foo) {return vals[foo]}`. Don't worry too much about it, make sure it works first, then you can externalise it – rath Jan 20 '20 at 14:02
0

Define your switch-like object:

const types = {
  typeA: 'some code',
  ...
}

Then call it:

types[type] || defaultType

You can also include the default value in the switch-lick object:

const types= {
  typeA: 'some code',
  ...,
  default: 'some code for default type',
}

Later:

types[type] || types.default

If you are using functions as values:

(types[type] && types[type](args)) || types.default()
elhoucine
  • 2,356
  • 4
  • 21
  • 37
0

You could combine the objects into a single one, the first one at last, to keep the values from the first and take type as accessor.

For using an alias, you need to specify a new property for it.

result = { ...objectB, ...objectA, typeC: objectA.typeA }[type] || defaultValue;
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392