mattyork / fuzzy

Filters a list based on a fuzzy string search
MIT License
831 stars 86 forks source link

Can I get Whole JSON object from "list" #18

Closed higunjan closed 8 years ago

higunjan commented 8 years ago

Hi, I'm using these module with node but I have long JSON Array contain object. I need matched object whole. Like

var list = [ {rompalu: 'baconing', zibbity: 'simba'} , {rompalu: 'narwhal' , zibbity: 'mufasa'} , {rompalu: 'a mighty bear canoe', zibbity: 'saddam hussein'} ];

I have above list of JSON Array and if I pass word narwhal than It's return only matched words in Array but I need array of matched object. output like :

[ {rompalu: 'narwhal' , zibbity: 'mufasa'} ]

mattyork commented 8 years ago

The returned results have an "original" property that will do what you want:

var list = [
    {rompalu: 'baconing', zibbity: 'simba'}
  , {rompalu: 'narwhal' , zibbity: 'mufasa'}
  , {rompalu: 'a mighty bear canoe', zibbity: 'saddam hussein'}
];
var options = {
    extract: function(el) { return el.rompalu; }
};
var results = fuzzy.filter('narwhal', list, options);
var matches = results.map(function(el) { return el.original; });
console.log(matches);                                                                                                                                                 
// [ { rompalu: 'narwhal', zibbity: 'mufasa' } ]