fusejs 搜索功能

最后更新于:2022-04-02 03:13:23

[TOC] > [github](https://github.com/krisk/fuse/) > [home](https://fusejs.io/) ## 安装 `npm install fuse.js` ## 完整 demo ``` // html
// js ``` ## 官方 demo ### 按ID搜索 搜索结果只返回 id 指定的值 ``` var books = [{ 'ISBN': 'A', 'title': "Old Man's War", 'author': 'John Scalzi' }, { 'ISBN': 'B', 'title': 'The Lock Artist', 'author': 'Steve Hamilton' }] var options = { keys: ['title', 'author'], id: 'ISBN' } var fuse = new Fuse(books, options) fuse.search('old') /**[ "A" ]*/ ``` ### 加权搜索 加权影响结果排名 ``` var books = [{ title: "Old Man's War fiction", author: 'John X', tags: ['war'] }, { title: 'Right Ho Jeeves', author: 'P.D. Mans', tags: ['fiction', 'war'] }] var options = { keys: [{ name: 'title', weight: 0.3 }, { name: 'author', weight: 0.7 }] }; var fuse = new Fuse(books, options) fuse.search('Man') /** [{ "title": "Right Ho Jeeves", "author": "P.D. Mans", "tags": ["fiction", "war"] }, { "title": "Old Man's War fiction", "author": "John X", "tags": ["war"] }] */ ```` ### 在字符串数组中搜索 数组中的值可以进行搜索 ``` var books = [{ 'title': "Old Man's War", 'author': 'John Scalzi', 'tags': ['fiction'] }, { 'title': 'The Lock Artist', 'author': 'Steve', 'tags': ['thriller'] }] var options = { keys: ['author', 'tags'] }; var fuse = new Fuse(books, options) fuse.search('tion') /**[{ "title": "Old Man's War", "author": "John Scalzi", "tags": ["fiction"] }]*/ ```
';