ReactJS / ES6:使用包含搜索日文文本?

时间:2018-03-12 18:03:40

标签: javascript ecmascript-6 cjk

所以,我正在编写客户端搜索,我需要查看日文字符串。我想知道如何正确地执行此操作?...即,我是否将文本格式更改为utf-8 something,然后搜索utf-8

示例:

我的所有数据都有japaneseData.title:"フェリーチェ三田" 当我输入我的search.value时:"フェ"使用japaneseData.title.includes(search.value)我没有得到匹配...

我该如何正确地做到这一点?

好的,经过进一步检查,评论是正确的,includes找到了子字符串。这一切都发生在filter()内部,我试图返回匹配的对象......

将我的代码更改为:

let filteredArrayofObjects = Lists.houseLists.filter(house => house.building_name.includes(query.search));

我回来了一些但不是全部。问题案例:

"アーバイルスパシエ芝浦BAY-SIDE".includes("エ芝浦"); // this evaluates to true, but does not get included in my filtered array...

好的,进一步挖掘,似乎问题是我需要在返回结果之前等待过滤过程......尚未找到解决方案。

async filter(arr, callback) {
    return (await Promise.all(
      arr.map(async item => {
        return (await callback(item)) ? item : undefined;
      })
    )).filter(i => i !== undefined);
  }

  handleFilterLists = async (query = {}) => {
    const { Lists } = this.props;

    let searchResults = await this.filter(Lists.houseLists, async house => {
      return house.building_name.includes(query.search);

       // the final evaluation to look similar to this:
       // var newArray = homes.filter(function (el) {
       // return el.price <= 1000 &&
       // el.sqft >= 500 &&
       // el.num_of_beds >=2 &&
       // el.num_of_baths >= 2.5;
       // });

    });

    this.setState({ searchResults });
  }

好的,所以,我尝试在过滤方法检查数组Lists.houseLists中的匹配对象后设置state.searchResults ...

1 个答案:

答案 0 :(得分:1)

如果检测到子字符串,则

includes返回true或false。如果您想要第一个检测到的子字符串开始的索引,请使用indexOf

我使用了您的示例源并使用includes搜索文本,并返回true。

编辑:

我使用了您的更新数据,这仍然有效。 https://codepen.io/anon/pen/RMWpwe

const sourceText = 'アーバイルスパシエ芝浦BAY-SIDE';
const searchText = 'エ芝浦';
const lists = [
  'スパシエ',
  '芝浦BAY-SIDE',
  'エ芝浦',
  'パシエ芝浦BAY'
];

console.log(lists.filter(item => item.includes(searchText)));
// ["エ芝浦", "パシエ芝浦BAY"]