arrays - Nested Category Search using underscore js -


this how data object http://jsfiddle.net/303tpltz/1

as may see here there categories inside categories must considered when search done

so problem can find top level names of categories using _.findwhere(result.response.categories, {name: 'arts & entertainment'})

but problem when need find mentioned inside supplied request, example if need find restaurant inside food >

can me deep search function please ?

my tangled solution jsfiddle:

function finddeep(cats, attrs) {     _.each(cats, function(data, i) {         var copy = data;         // delete copy.categories;         newarr.push(copy);         _.each(data.categories, function(newobj) {             var copy2 = newobj;             // delete copy2.categories;             newarr.push(copy2)              if (newobj.categories) {                 _.each(newobj.categories, function(otherobj) {                     var copy3 = otherobj;                     // delete copy3.categories;                     newarr.push(copy3)                 })             }         })     });      return _.findwhere(newarr, attrs) || null; } 

the problem data each node potentially should inspected further, means can't neatly apply filter every item, because skipped items might have nested categories need inspect.

however, using plain javascript or alternatively of _.reduce , bit of recursive magic, can job done little bit of code.

jsfiddle

function findmatchingcategories(cats, attrs) {     return _.filter(extractcategories(cats), attrs);      function extractcategories(cats) {         return _.reduce(cats, function (result, value) {             return result.concat([value]).concat(value.categories ? extractcategories(value.categories) : []);         }, []);     } }  console.log(findmatchingcategories(data, { name: 'tunnel' })); 

to explain:

_.reduce let's go through set of data , keep track of progressively 'reduced' data variable. in our case, reduce set of categories new array called result contains nested categories. make sure inspect nested categories well, recursively call extractcategories , use it's result add reduced result array.

finally left categories, nested or not, filter based on attr matches.


a more performant version less concatenation:

jsfiddle

function findmatchingcategories(cats, attrs) {     return _.filter(extractcategories(cats, []), attrs);      function extractcategories(currentcats, allcats) {         _.each(currentcats, function (value) {             allcats.push(value);             if (value.categories) {                 extractcategories(value.categories, allcats);             }         }, []);         return allcats;     } }  console.log(findmatchingcategories(data, { name: 'tunnel' })); 

the more go performance, less concise code becomes.

performance comparison of 3 approaches


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -