使用Fuse.js
时,我试图获取特定的数组对象。
这是我正在使用的有效载荷:
[
{
"name": "Alice",
"contacts": [
{
"name": "Bob",
"friends": [
{
"name": "Fooman",
"age": 40
},
{
"name": "Barman",
"age": 50
}
]
}
]
}
]
我想要得到的是该对象作为结果:
{
"name": "Barman",
"age": 50
}
这是我的搜索选项:
var options = {
id: "contacts.friends",
shouldSort: true,
tokenize: true,
matchAllTokens: true,
includeScore: true,
includeMatches: true,
threshold: 1,
location: 1,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: [
"contacts.friends.name"
]
};
var fuse = new Fuse(list, options); // "list" is the item array
var result = fuse.search("Bar");
我得到的结果是:
[
{
"item": {
"name": "Fooman", // returning the wrong object.
"age": 40
},
"matches": [
{
"indices": [
[
4,
4
]
],
"value": "Fooman",
"key": "contacts.friends.name",
"arrayIndex": 0
},
{
"indices": [
[
0,
2
],
[
4,
4
]
],
"value": "Barman",
"key": "contacts.friends.name",
"arrayIndex": 1
}
],
"score": 0.006866666666666667
}
]
似乎正在返回friends
数组中的第一项。