Fuse: fuse.js return specific item from nested json object

Created on 23 Jun 2020  路  3Comments  路  Source: krisk/Fuse

I am using fuse.js to fuzzy search a json dataset that looks 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.

question

All 3 comments

_For future reference, these types of questions would be better if asked on Stack Overflow, that way everyone can benefit, but also the question is bound to get wider reach (and thus an answer from someone other than me 馃槃 )._

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.

Oh, ok thanks, that worked perfectly!
p.s: I did ask on stack overflow here but did not get any response, so I came here 馃榾

Guess I'll answer your Q there as well 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

312307025 picture 312307025  路  4Comments

vexa picture vexa  路  4Comments

x1unix picture x1unix  路  3Comments

cekvenich picture cekvenich  路  4Comments

danielfdickinson picture danielfdickinson  路  4Comments