最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fuse.js search in array of strings - Stack Overflow

programmeradmin2浏览0评论

I was trying to implement fuse.js to my app where I have array of strings without any key.

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

When I try to configure the fuse.js I'm getting no results, because of unspecified key.

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

is it possible to perform fuzzy search on plain array, or do I need to convert everything to be an object?

I was trying to implement fuse.js to my app where I have array of strings without any key.

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

When I try to configure the fuse.js I'm getting no results, because of unspecified key.

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

is it possible to perform fuzzy search on plain array, or do I need to convert everything to be an object?

Share Improve this question asked Dec 4, 2019 at 14:42 Lukáš VáclavekLukáš Václavek 4521 gold badge6 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

It's possible to do a search on an array of strings. You need to not specify a keys property in the options object.

Here's an example:

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
发布评论

评论列表(0)

  1. 暂无评论