Preben Tjemsland
Preben Tjemsland

Reputation: 78

Fuse.js to search in array with dot in keys

Im using fetch on a Rest endpoint which gives me a array of objects. All the keys in the array has a dot in them. ex: {test.name: "test"}. I have to keep fetching new responses to get changes so while i can remove or replace the dot this will take some time/resources every time. Is there any way to use keys with dots in fuse.js?

I tried some variants of this with no luck.

const fuse = new Fuse(this.state.test, {
    keys: ['test.name']
});

ps. I cant change the keys in the Rest as its external

Upvotes: 1

Views: 1958

Answers (1)

krisk
krisk

Reputation: 7117

As of Fuse.js v6.3.0 you can search through nested values by providing the path via dot (.) or array notation. Therefore, if your key already has a dot in it, you can wrap it in an array:

const fuse = new Fuse(this.state.test, {
  // `test.name` is the actual key, so wrap it in an array
  keys: [['test.name']]
});

Upvotes: 3

Related Questions