javascript - return array defined by a key/value pair -


how filter return results according key/value pair in array.

say have array :

 .factory('dishesfactory', function (){ var factory = {     dishes :[  {nameenglish: 'tapenade',   namelocal: 'tapenade',   description: 'xxxxxx ',   region: 'provence-alpes-côte d\'azur',   regioncode: 'fr.b8',   itemid: 'fr002',   cuisinetypeisocode: 'fr',   dishcategory: 'entrée / appetizers',   country: 'france',   type: 'top_10'},   {nameenglish: 'greek style mushrooms  ',   namelocal: 'champignons à la grecque',   description: 'xxxxxxx',    region: 'all',   regioncode: '',   itemid: 'fr008',   cuisinetypeisocode: 'fr',   dishcategory: 'entrée / appetizers',  country: 'france',   type: ''}    // more entries    ]; 

and have following javascript either returns filtered-by-dishtype results user, or return whole list. want change returning whole list, list of item have type: 'top_10' value

.filter('selecteddishtype', function() { return function(dishes, dishtypes) {     console.log(dishtypes);     if (dishtypes.length > 0) {         return dishes.filter(function(dish) {             return dishtypes.indexof(dish.dishcategory.tolowercase()) != -1;         });     } else {          return dishes;      }    };  }) 

i have tried write proper syntax amending return:dishes, ne in js, cannot work properly... !

html edited (solution) coming across here, here solution had found :

   .filter('selecteddishtype', function() { return function(dishes, dishtypes) { // returns ad hoc categories selected user     console.log(dishtypes);     if (dishtypes.length > 0) {         return dishes.filter(function(dish) {             return dishtypes.indexof(dish.dishcategory.tolowercase()) != -1;         });     } else {          return dishes.filter(function(dish){ // returns "type =='top_10' avoid long loading time             return dish.type;         });       }     }; }) 

change filter function to

return dishes.filter(function(dish){   return dish.type.tolowercase().indexof(dishtypes) != -1; }); 

i think code work


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -