Match multiple fields
MatthiasF999 opened this issue · comments
match-sorter
version:2.3.0
I love this package, but I usually want to filter through multiple fields.
const objList = [
{name: 'Janice', color: 'Green'},
{name: 'Fred', color: 'Orange'},
{name: 'George', color: 'Blue'},
{name: 'Jen', color: 'Red'},
]
matchSorter(objList, 'ge bl', {keys: ['name', 'color']})
This example results in []
, but I would expect the result to be [{name: 'George', color: 'Blue'}]
.
So it only tries to match one field, and ignores the other fields.
I'm confused... So you want the search to work across fields?
Yes exactly. I usually work with huge lists of objects and want to sort and filter across multiple fields with one input..
Hey @MatthiasF999,
You could do this:
function matchSorterAcrossKeys(list, search, options) {
const joinedKeysString = item => options.keys.map(key => item[key]).join(' ')
return matchSorter(list, search, {
...options,
keys: [...options.keys, joinedKeysString],
})
}
const objList = [
{name: 'Janice', color: 'Green'},
{name: 'Fred', color: 'Orange'},
{name: 'George', color: 'Blue'},
{name: 'Jen', color: 'Red'},
]
const matches = matchSorterAcrossKeys(objList, 'ge bl', {keys: ['name', 'color']})
// matches is: [{name: 'George', color: 'Blue'}]
This is not a typical use case so we wont be supporting it any better than that. I personally feel like this is a pretty good solution though. Good luck!