dot-wild icon indicating copy to clipboard operation
dot-wild copied to clipboard

Filter function

Open emahuni opened this issue 7 years ago • 0 comments

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

emahuni avatar Aug 16 '18 07:08 emahuni