Search code examples
reactjsecmascript-6lodash

Match prop values to lookup list with new (pretty) values


I'm using react and lodash. I have a prop type {props.value} that returns the result of "ThisIsFoo" and other results such as "ThisIsBar". I want to be able to map these results to elegantly display the output as "This is foo". Some sort of look up array will do the trick where I can list all the possible outcomes. e.g.

    var KeyMap = {
        ThisIsFoo : "This is foo",
        ThisIsBar : "This is Bar",
        anotherOutcomeHere: "Another outcome here"
    };

Ideally I want to get lodash to look at {props.value} match them to the key is it exists then output with its replacement.


Solution

  •         var KeyMap = {
            slowFit : "Slow Fit",
            fastFit : "Fast Fit",
            slowUnfit : "Slow Unfit",
            fastUnfit : "Fast Unfit",
    
        };
    
    const str = props.label.replace(/slowFit|fastFit|slowUnfit|fastUnfit/gi, function(matched){
      return KeyMap[matched];
    });