【问题标题】:Fuse.js search in array of stringsFuse.js 在字符串数组中搜索
【发布时间】:2019-12-04 14:42:44
【问题描述】:

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

['Kelly', 'Creed', 'Stanley', 'Oscar', 'Michael', 'Jim', 'Darryl', 'Phyllis', 'Pam', 'Dwight', 'Angela', 'Andy', 'William', 'Ryan', 'Toby', 'Bob']

当我尝试配置 fuse.js 时,我没有得到任何结果,因为未指定密钥。

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("");

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

【问题讨论】:

    标签: javascript fuse.js


    【解决方案1】:

    可以对字符串数组进行搜索。您无需在选项对象中指定keys 属性。

    这是一个例子:

    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
    

    【讨论】:

    • 如果你有.js文件,你也可以使用 let colordatavalues = Object.values(list); const fuse = new Fuse(colordatavalues, options);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 2023-03-13
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多