brianreavis / sifter.js

A library for textually searching arrays and hashes of objects by property (or multiple properties). Designed specifically for autocomplete.
1.09k stars 125 forks source link

Add option for searching through nested properties #16

Open levacic opened 9 years ago

levacic commented 9 years ago
var data = [
    {
        "title": "Foo",
        "tags": ["foo", "bar", "baz"]
    },
    {
        "title": "Bar",
        "tags": ["bar", "qux"]
    }
];

It would be great to have the ability to search the data based on the tags property. It's possible to hack this up in user-code by concatenating the tags property into a string, with a unique imploding character, e.g.

var data = [
    {
        "title": "Foo",
        "tags": ["foo", "bar", "baz"],
        "_tags": "foo_bar_baz"
    },
    {
        "title": "Bar",
        "tags": ["bar", "qux"],
        "_tags": "bar_qux"
    }
];

...and search through that transformed data instead, but this is neither a clean nor a stable solution.

hagabaka commented 9 years ago

Maybe the search/sort "field" options can be generalized to take a function to map each item, for example

search('bar', fields: [
  function(item, term) {
    return item.tags.indexOf(item) >= 0;
  },
  'title'
]);

or

search('bar', fields: [
  function(item) {
    return item.tags.join('_');
  },
  'title'
]);