简体   繁体   中英

How to set fuse.js options

 const JSON = [ { "name": "01,02" }, { "name": "01" }, { "name": "05" }, { "name": "06,09" }, { "name": "04,05" }, { "name": "02,03" }, { "name": "02,04,05" }, { "name": "01,02" }, { "name": "01,03" } ] function foo (str) { const options = { keys: ['name'], threshold: 0, location: 0, distance: 100, minMatchCharLength: str.length } const _fuse = new Fuse(JSON, options) console.log(_fuse.search(str)) } foo('03')
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/3.2.1/fuse.min.js"></script>

I am trying to use fuse.js for filtering. I am unsure of how to use the options. If I search with 03 I am not returning any results. I have tried changing the location and threshold but not completely understanding it. I set the minMatchCharLength to the length of the query string thinking that would prevent a query of 01 from returning items that only contain a 0 . Is that correct?

Your code is fine already. You set threshold to 0 which means

fuse requires a perfct match (of both letters and location)

And there is no name property which is exactly 03 .

So if you fiddle just with threshold and set it to 0.2 for example - you will get some results from search:

[{name: "02,03"}, {name: "01,03"}]

try to change "name" to name :

const JSON =  [
  {
    name: "01,02"
  },
  {
    name: "01"
  },
  {
    name: "05"
  },
  {
    name: "06,09"
  },
  {
    name: "04,05"
  },
  {
    name: "02,03"
  },
  {
    name: "02,04,05"
  },
  {
    name: "01,02"
  },
  {
    name: "01,03"
  }
];

Here you can find a demo that allows you to 'play' with the configurations. You can paste your json and try to get different results: Link

For more information about the configuration, you can refer to step 2 of the previous link, which explains all the options. Eg: threshold : At what point does the match algorithm give up. A threshold of 0.0 requires a perfect match (of both letters and location), a threshold of 1.0 would match anything

Here a demo with your example : jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM