Advertisement
svetlai

find

Jul 10th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function(options) {
  2.     //console.log(this.items);
  3.     var result = [];
  4.     if (!options || (!isNumber(options) && !isObject(options))) {
  5.         throw new Error('You must provide a valid id or options.');
  6.     }
  7.  
  8.     if (isNumber(options)) {
  9.         result = this.items.filter(function(item) {
  10.             return item.id == options;
  11.         });
  12.  
  13.         if (result.length > 0) {
  14.             return result[0];
  15.         } else {
  16.             return null;
  17.         }
  18.  
  19.     } else {
  20.         var isFound = true;
  21.         result = this.items.filter(function(item) {
  22.             //item.id = item._id;
  23.             //item.genre = item._genre;
  24.             //item.name = item._name;
  25.             for (var prop in options) {
  26.                 if (options[prop].toString().toLowerCase() != item[prop].toString().toLowerCase()) {
  27.                     isFound = false;
  28.                     break;
  29.                 }
  30.             }
  31.  
  32.             if (isFound) {
  33.                 return item;
  34.             }
  35.         });
  36.     }
  37.  
  38.     return result;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement