Closed skube closed 4 years ago
name.0.first
will allow you to match Janice
or Fred
with that data. This is because the 0
in that key string is referencing the first entry in the name
array of each object.
If you want to match any of the number of arrays, then you should use the property callback. Here's the example from the docs:
const list = [{name: 'Janice'}, {name: 'Fred'}, {name: 'George'}, {name: 'Jen'}]
matchSorter(list, 'j', {keys: [item => item.name]})
// [{name: 'Janice'}, {name: 'Jen'}]
Thanks for responding. Your example does work for only one property. I must be dense because I still can't get it work with more complicated structures.
I've created a sandbox to illustrate
A co-worker actually figured it out, so given:
const nestedObjList = [
{
name: [
{ first: 'Janice', last: 'Smith' },
{ first: 'Jon', last: 'Doe' }
],
},
{
name: [
{ first: 'Fred', last: 'Astaire' },
{ first: 'Jenny', last: 'Doe' },
{ first: 'Wilma', last: 'Flintstone' },
],
},
];
You need to use map
:
matchSorter(nestedObjList, 'doe', {
keys: [
item => item.name.map(i => i.first),
item => item.name.map(i => i.last),
],
})
@kentcdodds I would recommend updating the docs to make it more clear for this kind of use case. 😃
match-sorter
version: 4.0.1node
version: v10.15.3yarn
version: 1.19.0The example provided in the docs is as follows (and only has one item in each
name
array):I have a similar structure, however mine only seems to match the first entry in the array.
Am I doing something wrong?