简体   繁体   English

有条件地应用字符串修饰符方法

[英]Conditionally apply string modifier method

Is it possible to conditionally apply a string modifier method such as .toLowerCase() in a more clever method than doing:是否有可能有条件地应用字符串修饰符方法,例如.toLowerCase()更聪明的方法:

const text = 'ThIs Is SoMe TeXt';
const maybeConvert = (text, toLower = false) => {
    return toLower ? text.toLowerCase() : text;
}

You can do something like this if you only want to do something if condition is true and don't wanna do anything if it's false如果你只想在条件为真时做某事而在条件为假时不想做任何事情,你可以做这样的事情

  const text = 'ThIs Is SoMe TeXt';
    const maybeConvert = (text, condition) => {
     return condition  && text.toLowerCase();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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