在不处理数据库的情况下解决相同的问题

Keep the same issue resolved without taking care of the Database

本文关键字:问题 解决 情况下 处理 数据库      更新时间:2023-09-26

我遇到了一个问题,我的客户一直要求更改,每次DB人员进行更改时,我也需要在前端进行更改,所以我需要更动态地进行更改。

我有这个

  if (data.sideBetName === 'SuitEmUp') {
    // something happens here
  }

所以,'SuitEmUp'是来自DB的字符串,现在,客户端要求在前端显示为Suit Em Up而不是'SuitEmUp',那么,我如何告诉JavaScript,如果字符串以Suit开头,那么就要考虑它?否则,忽略它…

使用正则表达式:

if (/^Suit/.test(data.sideBetName) === true) {
    // something happens here
}

这将匹配任何以"Suit"开头的字符串,例如"Suit em up"

如果您想使这个大小写不敏感,您可以使用:/^Suit/i

您可以使用indexOf方法检查字符串的开始,如下所示

if(data.sideBetName.toUpperCase().indexOf('SUIT') == 0){
  //do your logic
}

你可以做:

if ((data.sideBetName).slice(0,4) === 'Suit') { // stuff}

它只需要字符串的第一个单词的副本,因此这对Suit Em Up和Suit EmUp 都有效