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.

How I can search in the nested array of strings?

See original GitHub issue

Here is my data:

[
  [
    'test1',
    'test2',
    'test3-match',
  ],
  [
    'test11',
    'test12-match',
    'test13',
  ],
  [
    'test21',
    'test22',
    'test23',
  ],
]

How I can search this array? I need to return only subarrays that include any matches.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
BenJenkinsoncommented, May 4, 2022

I don’t think fuse.js can understand the structure of your array as it is, but I was able to search it by transforming it first.

For example, given your array of arrays, of type string[][]:

[
  ["test1", "test2", "test3-match"],
  ["test11", "test12-match", "test13"],
  ["test21", "test22", "test23"],
];

You can map them first, to type { foo: string[] }[], e.g.:

[
  { foo: ["test1", "test2", "test3-match"] },
  { foo: ["test11", "test12-match", "test13"] },
  { foo: ["test21", "test22", "test23"] }
]

… and then feed that to fuse.js, like so:

// Your original list.
const list = [
  ["test1", "test2", "test3-match"],
  ["test11", "test12-match", "test13"],
  ["test21", "test22", "test23"],
];

// Map the list to a structure that `Fuse.js` can understand.
const mapped = list.map((x) => {
  return {
    foo: x,
  };
});

// Specify the `key` option, with the placeholder property name we want to search.
const options = {
  keys: ["foo"],
};

const fuse = new Fuse(mapped, options);

// Do the search..
return fuse.search("match");

The results would look like this:

[
  {
    "item": {
      "foo": ["test1", "test2", "test3-match"]
    },
    "refIndex": 0
  },
  {
    "item": {
      "foo": ["test11", "test12-match", "test13"]
    },
    "refIndex": 1
  }
]

0reactions
github-actions[bot]commented, Apr 9, 2022

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

Read more comments on GitHub >

github_iconTop Results From Across the Web

search array of string through nested array of data [duplicate]
I need to implement a search query on a nested array of data. The user can input multiple search strings separated by a...
Read more >
Nested Array: Filter on a String [] /Map doesn't return results as ...
Here is the object structure. I should be able to search by carrier Name or provider first/last Name. And If I search by...
Read more >
Nested field type | Elasticsearch Guide [8.5] | Elastic
The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way...
Read more >
Querying arrays with complex types and nested structures
The following examples illustrate how to search a dataset for a keyword within an element inside an array, using the regexp_like function. It...
Read more >
Array.prototype.flat() - JavaScript - MDN Web Docs
However, its elements must be arrays if they are to be flattened. Examples. Flattening nested arrays. const arr1 = [ ...
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