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.

Is it possible to search through a list of objects?

See original GitHub issue

One of the big reasons why I use Fuse is that I can pass a list of objects and it will search on a subset of keys. This requires additional indexing, I assume. Is it possible to do this with uFuzzy? It seems to only accept string[]

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
leeoniyacommented, Oct 12, 2022

something like this (incomplete):

const uFuzzy = require('../dist/uFuzzy.cjs');

let recs = [
    {
        foo: "Martha came to the conclusion that shake weights are a great gift for any occasion.",
        bar: "his seven-layer cake only had six layers.",
        baz: "The urgent care center was flooded with patients after the news of a new deadly virus was made public.",
    },
    {
        foo: "I never knew what hardship looked like until it started raining bowling balls.",
        bar: "It didn't make sense unless you had the power to eat colors.",
        baz: "For oil spots on the floor, nothing beats parking a motorbike in the lounge.",
    },
    {
        foo: "They were excited to see their first sloth.",
        bar: "The rusty nail stood erect, angled at a 45-degree angle, just waiting for the perfect barefoot to come along.",
        baz: "She wanted a pet platypus but ended up getting a duck and a ferret instead.",
    },
    {
        foo: "They say that dogs are man's best friend, but this cat was setting out to sabotage that theory.",
        bar: "He poured rocks in the dungeon of his mind.",
        baz: "He was willing to find the depths of the rabbit hole in order to be with her.",
    },
    {
        foo: "When I was little I had a car door slammed shut on my hand and I still remember it quite vividly.",
        bar: "She advised him to come back at once.",
        baz: "A purple pig and a green donkey flew a kite in the middle of the night and ended up sunburnt.",
    },
    {
        foo: "She looked at the masterpiece hanging in the museum but all she could think is that her five-year-old could do better.",
        bar: "We should play with legos at camp.",
        baz: "Trash covered the landscape like sprinkles do a birthday cake.",
    },
    {
        foo: "It was obvious she was hot, sweaty, and tired.",
        bar: "As he waited for the shower to warm, he noticed that he could hear water change temperature.",
        baz: "It took him a while to realize that everything he decided not to change, he was actually choosing.",
    },
    {
        foo: "She moved forward only because she trusted that the ending she now was going through must be followed by a new beginning.",
        bar: "Fluffy pink unicorns are a popular status symbol among macho men.",
        baz: "I am counting my calories, yet I really want dessert.",
    },
    {
        foo: "Happiness can be found in the depths of chocolate pudding.",
        bar: "Strawberries must be the one food that doesn't go well with this brand of paint.",
        baz: "Joe made the sugar cookies; Susan decorated them.",
    },
    {
        foo: "Poison ivy grew through the fence they said was impenetrable.",
        bar: "Nothing is as cautiously cuddly as a pet porcupine.",
        baz: "Dan ate the clouds like cotton candy.",
    },
];

let haystack = recs.map(r => `${r.foo}¦${r.bar}¦${r.baz}`);

let needle = 'was';

let uf = new uFuzzy();

let idxs = uf.filter(haystack, needle);

console.log(idxs.map(i => recs[i]));
1reaction
leeoniyacommented, Oct 10, 2022

out of the box, no. but with bit of cleverness, yes.

the idea is to create a string[] by concat’ing the fields you’re interested in, using some rarely-used separator char like ¦. this effectively becomes your “index”. once you have this list you can use uFuzzy as normal. it’s not a perfect solution, but i think it will work quite well.

it would take a bit more code to get the highlight ranges (info.ranges) properly partitioned based on offsets from prior ¦, but may not be necessary if you don’t intend to do partial match highlighting in the UI.

this use case is on my demos TODO list, PRs welcome 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Searching a list of objects in Python - Stack Overflow
I'm trying to figure out how to search a list of objects for objects with an attribute equaling a certain value. Below is...
Read more >
Searching a list of objects in Python - GeeksforGeeks
Searching for a particular or group of objects can be done by iterating through a list. where,
Read more >
Searching or Sorting a list of Objects Based on an Attribute in ...
Often, when you have a group of objects, you might be looking to sort them based not on their location in memory, but...
Read more >
List<T>.Find(Predicate<T>) Method - Microsoft Learn
Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List<T>.
Read more >
How to Find an Element in a List with Java - Baeldung
Java itself provides several ways of finding an item in a list: The contains method; The indexOf method; An ad-hoc for loop; The...
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