question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Search in Array<String> returns indexes instead of items

See original GitHub issue

Hello, I used Live Demo page to check a search in flat array:

const list = [
  { value: "85626400800" },
  { value: "81323902061" },
  { value: "8079842032" },
  { value: "7825731309" },
  { value: "9492224473" },
  { value: "81323902062" },
  { value: "71509915863" },
  { value: "84718105204" },
  { value: "81323902013" },
  { value: "80487910890" },
]

var options = {
  shouldSort: true,
  tokenize: true,
  matchAllTokens: true,
  threshold: 0,
  location: 0,
  distance: 0,
  maxPatternLength: 32,
  minMatchCharLength: 1,
  keys: [
    "value"
  ]
};
var fuse = new Fuse(list, options); // "list" is the item array
var result = fuse.search("804");

/* SEARCH RESULTS */
[
  {
    "value": "80487910890"
  }
]

But when I try to search by flat list it returns indexes instead of items

const list =  [
  "85626400800",
  "81323902061",
  "8079842032",
  "7825731309",
  "9492224473",
  "81323902062",
  "71509915863",
  "84718105204",
  "81323902013",
  "80487910890",
]

// Options without a "key" property
var options = {
  shouldSort: true,
  tokenize: true,
  matchAllTokens: true,
  threshold: 0,
  location: 0,
  distance: 0,
  maxPatternLength: 32,
  minMatchCharLength: 1,
};
var fuse = new Fuse(list, options); // "list" is the item array
var result = fuse.search("804");

/* SEARCH RESULTS */
[
  9
]

Looks like issue is here — https://github.com/krisk/Fuse/blob/master/src/index.js#L136 Why is that?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:8

github_iconTop GitHub Comments

2reactions
heyimalexcommented, Jul 12, 2019

It’s probably just a weird decision that was made a long time ago. Not so hard to get around:

// Fuse has a quirk where, when the input is an array of strings instead of
// objects, it only returns the index to each result. That means we need to
// re-map those indexes to the original list items after the search completes.
var result = fuse.search("804").map(i => list[i]);
1reaction
heyimalexcommented, Jul 25, 2019

I wonder if it’s possible to type correctly? Maybe with conditional types.

export interface FuseResult<T> {
    item: T extends string ? number : T;
    matches?: any;
    score?: number;
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Search Array Elements For Substring and return index
I am trying to search through each element in an Array to find if it contains a substring. If it does I want...
Read more >
Four Methods to Search Through Arrays in JavaScript
Learn about four approaches to searching for values in arrays: includes, indexOf, find, and filter methods.
Read more >
Indexed collections - JavaScript - MDN Web Docs - Mozilla
The indexOf() method searches the array for searchElement and returns the index of the first match. const a = ['a', 'b', ...
Read more >
5. Working with Arrays and Loops - JavaScript Cookbook [Book]
Arrays in JavaScript are zero-based, which means the first element index is zero, ... It returns a string with all of the array...
Read more >
array_search - Manual - PHP
array_search — Searches the array for a given value and returns the first ... values, use array_keys() with the optional search_value parameter instead....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found