首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字符串数组中的Fuse.js搜索

字符串数组中的Fuse.js搜索
EN

Stack Overflow用户
提问于 2019-12-04 22:42:44
回答 1查看 2.2K关注 0票数 7

我试图在我的应用程序中实现fuse.js,其中我有没有任何键的字符串数组。

代码语言:javascript
复制
['Kelly', 'Creed', 'Stanley', 'Oscar', 'Michael', 'Jim', 'Darryl', 'Phyllis', 'Pam', 'Dwight', 'Angela', 'Andy', 'William', 'Ryan', 'Toby', 'Bob']

当我尝试配置fuse.js时,由于未指定的键,我没有得到任何结果。

代码语言:javascript
复制
var options = {
  shouldSort: true,
  threshold: 0.6,
  location: 0,
  distance: 100,
  maxPatternLength: 32,
  minMatchCharLength: 1,
  keys: [
    "title",
    "author.firstName"
  ]
};
var fuse = new Fuse(list, options); // "list" is the item array
var result = fuse.search("");

是否可以在普通数组上执行模糊搜索,或者我是否需要将所有内容转换为对象?

EN

回答 1

Stack Overflow用户

发布于 2020-04-29 10:08:00

可以对字符串数组进行搜索。不需要在options对象中指定keys属性。

下面是一个例子:

代码语言:javascript
复制
const list = ['Kelly', 'Creed', 'Stanley'];

// your options can be anything you want, but don't include
// the keys property
let options = {
  shouldSort: true,
  threshold: 0.6,
  location: 0,
  distance: 100,
  maxPatternLength: 32,
  minMatchCharLength: 1,
  // don't include the keys property
};

const fuse = new Fuse(list, options);

let result = fuse.search('Kelly');
// result will be:
// {"item":"Kelly","refIndex":0}
// here, refIndex is the index of the element in list
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59178597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档