代码之家  ›  专栏  ›  技术社区  ›  James Ives

数组reduce未返回预期结果

  •  0
  • James Ives  · 技术社区  · 7 年前

    我正在研究一个搜索函数,该函数将通过数组过滤器,然后是reduce以匹配正确的字符串,但我在获得预期结果时遇到了一些困难。

    我有一个公共短语的枚举,我想映射到它们的值,例如如果 Amer 被搜索它也应该匹配 American . 为了实现这一点,我设置了一个对象,其中一些值的reduce值应该将该值添加到搜索字符串中,例如,如果API数据或搜索数据包含 U Street African Amer 它应该变成 u street african amer american 在返回。如果找不到任何映射数据,则应返回原始函数参数。

    const acronymEnum = {
      MT: 'Mountain',
      AMER: 'American',
      PL: 'Place',
      UDC: 'Univeristy of the District of Columbia',
      AU: 'American University',
      AVE: 'Avenue',
      CUA: 'Catholic University of America',
      NOMA: 'North of Massechusets Avenue',
      GMU: 'George Mason University',
      VT: 'Virginia Tech',
      UVA: 'University of Virginia',
      RAEGAN: 'DCA',
      ST: 'Street',
      SW: 'South West',
      SQ: 'Square',
      PENN: 'Pennsylvania',
    };
    
    function convertStationAcronym(stationName) {
      return Object.keys(acronymEnum).reduce((previous, current) => {
        if (stationName.toLowerCase().includes(current.toLowerCase())) {
          console.log('trigger the match', current)
          return stationName.concat(` ${acronymEnum[current]}`).toLowerCase();
        } else {
         return previous.toLowerCase();
        }
      }, '');
    }
    
    console.log(convertStationAcronym('U Street African Amer'))

    我遇到的问题是,它返回未定义的,或者根据传递给函数的内容,只向返回添加一个映射短语。任何帮助都将不胜感激!

    2 回复  |  直到 7 年前
        1
  •  2
  •   Bergi    7 年前

    你要开始 reduce 具有 stationName 作为初始值,然后始终连接到 previous 不是原来的 台名 价值观:

    const acronymEnum = {
      MT: 'Mountain',
      AMER: 'American',
      PL: 'Place',
      UDC: 'Univeristy of the District of Columbia',
      AU: 'American University',
      AVE: 'Avenue',
      CUA: 'Catholic University of America',
      NOMA: 'North of Massechusets Avenue',
      GMU: 'George Mason University',
      VT: 'Virginia Tech',
      UVA: 'University of Virginia',
      RAEGAN: 'DCA',
      ST: 'Street',
      SW: 'South West',
      SQ: 'Square',
      PENN: 'Pennsylvania',
    };
    
    function convertStationAcronym(stationName) {
      return Object.keys(acronymEnum).reduce((previous, currentKey) => {
        const re = new RegExp("\\b" + currentKey + "\\b", "i"); // TODO: escape special characters
        if (re.test(stationName)) {
          console.log('trigger the match', currentKey)
          return previous.concat(' ', acronymEnum[currentKey]);
    //           ^^^^^^^^
        } else {
         return previous;
        }
      }, stationName).toLowerCase();
    //   ^^^^^^^^^^^
    }
    
    console.log(convertStationAcronym('U Street African Amer'))
        2
  •  1
  •   Mohammed Ashfaq    7 年前

    解决这个问题的另一种方法是将输入字符串划分为一个数组,对于数组中的每个元素,我们将在AcronymEnum对象中找到条目。如果输入字符串的长度较小,这种方法将更有效。

    const acronymEnum = {
      MT: 'Mountain',
      AMER: 'American',
      PL: 'Place',
      UDC: 'Univeristy of the District of Columbia',
      AU: 'American University',
      AVE: 'Avenue',
      CUA: 'Catholic University of America',
      NOMA: 'North of Massechusets Avenue',
      GMU: 'George Mason University',
      VT: 'Virginia Tech',
      UVA: 'University of Virginia',
      RAEGAN: 'DCA',
      ST: 'Street',
      SW: 'South West',
      SQ: 'Square',
      PENN: 'Pennsylvania',
    };
    
    function convertStationAcronym(stationName) {
      let stationNameArray = stationName.split(" ");
      let result =  stationNameArray.map((item)=> { 
        return acronymEnum[item.toUpperCase()] 
          ? acronymEnum[item.toUpperCase()]
          :item 
      })
      return result.join(" ");
    }
    
    console.log(convertStationAcronym('U Street African Amer'))