fuse.js return specific item from nested json object

3.2k Views Asked by At

I am using fuse.js to fuzzy search a json dataset that looks something like this:

const dataSet = [
    {
      title: "a list",
      cards: [
        {
          title: "a card",
        },
        {
          title: "another card",
        },
        {
          title: "a third card",
        },
      ],
    },
...
];

When I perform a search for a card title:

const fuse = new Fuse(dataSet, { keys: ['cards.title']})
console.log(fuse.search("a third card"))

the list that contains the card is returned

{
   title: "a list",
   cards: [
      {id: 1, title: "a card"},
      ...
   ]
}

I want the data to return the specific card that has the title "a third card". Is there any way I can do this?

I looked into the docs, and found a getFn option that might do the job, but I couldn't get it to work. I also know that I can achieve this by flattening my object, but I don't want to do that as it adds complexity for my use case.

1

There are 1 best solutions below

0
krisk On BEST ANSWER

Sounds like what you could do is set the option includeMatches to true. This would also give you the exact item that was matched in the nested sub-array.