lodash utility

by Akram kamal

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.6.0/lodash.min.js"></script>

JavaScript

var array = [1, 2, 3, 4];
var evens = _.remove(array, function(n) {
  return n % 2 == 0;
});

console.log(array);
// → [1, 3]

console.log(evens);
// → [2, 4]
// --------------------------------------------------

var users = [
  { 'user': 'barney',  'age': 36, 'active': true },
  { 'user': 'fred',    'age': 40, 'active': false },
  { 'user': 'pebbles', 'age': 1,  'active': true }
];

var whois = _.result(_.find(users, function(chr) {
  return chr.age < 40;
}), 'user');
// → 'barney'
console.log(whois);

// using the `_.matches` callback shorthand
_.result(_.find(users, { 'age': 1, 'active': true }), 'user');
// → 'pebbles'

// using the `_.matchesProperty` callback shorthand
_.result(_.find(users, 'active', false), 'user');
// → 'fred'

// using the `_.property` callback shorthand
_.result(_.find(users, 'active'), 'user');
// → 'barney'