Author: Not specified Language: javascript
Description: Not specified Timestamp: 2018-04-17 10:28:22 +0000
View raw paste Reply
  1. // say I have an array with some data like this
  2. var data = ['talk', 'run', {action: 'walk'}, {action: 'sing'}, {action: 'dance'}],
  3.  
  4. // sure I can make my own methods that
  5. // make use of closure...
  6. findProperty = function (propName) {
  7.  
  8.     // ...that when called pass a function that
  9.     // _.find can use
  10.     return function (el, i, col) {
  11.  
  12.         if (typeof el === 'object') {
  13.  
  14.             return propName in el;
  15.  
  16.         }
  17.  
  18.     };
  19.  
  20. };
  21.  
  22. // and find will use it.
  23. console.log( _.find(data, findProperty('action')) ); // {action:'walk'}
  24.  
  25. // but there is a lodash method for that to begin with
  26. // called _.property
  27. console.log( _.find(data, _.property('action') ) );  // {action:'walk'}
  28.  
  29. // and why even bother with that when there is
  30. // a short hand for it.
  31. console.log( _.find(data, 'action') );  // {action:'walk'}
View raw paste Reply