javascript object instead of switch

Solutions on MaxInterview for javascript object instead of switch by the best coders in the world

showing results for - "javascript object instead of switch"
Lucas
10 Sep 2016
1const dogSwitch = (breed) => ({
2  "border": "Border Collies are good boys and girls.",
3  "pitbull": "Pit Bulls are good boys and girls.",
4  "german": "German Shepherds are good boys and girls."
5})[breed]
6dogSwitch("border") // "Border Collies are good boys and girls."
Luis
22 Feb 2020
1function getDrink (type) {
2  var drinks = {
3    'coke': function () {
4      return 'Coke';
5    },
6    'pepsi': function () {
7      return 'Pepsi';
8    },
9    'lemonade': function () {
10      return 'Lemonade';
11    },
12    'default': function () {
13      return 'Default item';
14    }
15  };
16  return (drinks[type] || drinks['default'])();
17}
Erica
03 Jul 2017
1function getSnack (type) {
2  var snack;
3  function isDrink () {
4    return snack = 'Drink';
5  }
6  function isFood () {
7    return snack = 'Food';
8  }
9  var snacks = {
10    'coke': isDrink,
11    'pepsi': isDrink,
12    'cookies': isFood,
13    'crisps': isFood,
14  };
15  return snacks[type]();
16}
17
18var snack = getSnack('coke');
19console.log(snack); // 'Drink'
queries leading to this page
switch case with objectconvert switch statements into object javascriptswitch object javascriptswitch case in javascript objectswitch case in obj javscriptswitch js with objectobject in switch case javascriptjs switch objectjs use object instead of switchusing objects as switch caseconvert switch statements into objectjavascript using switch to assign value to objectswitch case js objectjs switch with objecthow to turn a switch into an objectobject value switch caseobject switch javascriptjavascript switch statement objectswitch with instance of jsjavascript switch case onjectuse switch statement in object jsjs object switches6 javascript return object with switch js switch check objectswitch case objectusing an object as a switchjs switch for type of objectjs switch case or objectjavascript switch objectswitch case check for object available in javascriptjavascript return object with switchswitch objectswitch statement with object javascriptusing object lierals instead of switchgenerate switch case from object javascriptobject instead of case switchjavascript use object instead of switchswitch case object javascriptchange object inside switch case jscan you create object in case switchreturn objects with switch statementsadd object property inside switch case jsswitch return object jsswitch statement object jsobject instead switchswitch case object jsjs switch case object valueswitch case in object javascriptjavascript switch case objectts switch for type of objectjavascript object instead of switchaddobject inside switch case jsjs switch case on objectjavascript object instead of switch