dot-wild
dot-wild copied to clipboard
Filter function
Ok so the get function is there, but it gets the end obj/arr. A filter function will be great that does this:
const members = [
{ username: 'gumbouser', profile: { age: 33 } },
{ username: 'tsuyoshiwada', profile: { age: 24 } },
{ username: 'sampleuser', profile: { age: 30 } },
{ username: 'foobarbaz', profile: { age: 33 } }
];
dot.filter(members, '*.age', 33);
// => [ { username: 'gumbouser', profile: { age: 33 } },
// { username: 'foobarbaz', profile: { age: 33 } } ]
returns the objects that have hits from the root.
Implementation is simple here (sketch):
Filter function. takes in obj (object to filter) and path (the dot-wild path to use)
// filter function
return obj.filter(o => dot.has(o, path));
the path given can/should take into account that this is an inner part of the obj