因此,我正在编写客户端搜索,并且需要查看日语字符串。我想知道如何正确执行此操作?...即我是否将文本格式更改为 utf-8 some
,然后搜索 utf-8
?
示例:
我的所有数据都有 japaneseData.title
:“feiriーチェ三田”
当我使用 japaneseData.title.includes(search.value)
输入 search.value
为:“fuェ”时,我没有得到匹配项...
如何正确执行此操作?
好的,经过进一步检查,注释是正确的,并且 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
...
最佳答案
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"]
关于javascript - ReactJS/ES6 : Searching Japanese text using includes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49241742/