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

ReactJS / ES6: Searching Japanese text using includes?

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

示例:

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

如何正确执行此操作?

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

将我的代码更改为:

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...

好的,进一步挖掘,问题似乎是我需要等待过滤过程才能return结果...还没有找到解决方案。

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

includes returns true 或 false 是否检测到子字符串。如果您想要第一个检测到的子字符串开始位置的索引,请使用 indexOf.

我使用了您的样本来源和搜索文本 includes,它 returns 正确。

编辑:

我使用了您的更新数据,这仍然有效。 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"]