21

I have been trying to return a property of an object by filtering it first. Here's what I did:

var characters = [
  { 'name': 'barney',  'age': 36, 'blocked': false },
  { 'name': 'fred',    'age': 40, 'blocked': true },
  { 'name': 'pebbles', 'age': 1,  'blocked': false }
];

_.find(characters, function(chr) {
     return  chr.age == 40
});

It returns whole object where as I want to return specific property. Can anyone guide me how can I do it?

Any help will be appreciated.

2
  • Just access the property age after you filtered the object you need.
    – elclanrs
    Aug 26, 2014 at 18:44
  • 2
    i can do that but i wanted to be done in method .
    – Salman
    Aug 26, 2014 at 18:45

4 Answers 4

53

You could use the Lodash chaining ability. As its name implies, it enables you to chain Lodash methods calls. _.filter and _.map are appropriate here:

const characters = [
  { 'name': 'barney',  'age': 36, 'blocked': false },
  { 'name': 'fred',    'age': 40, 'blocked': true  },
  { 'name': 'pebbles', 'age': 1,  'blocked': false },
]

const names = _(characters)
  .filter(c => c.age < 40)
  .map('name')
  .value()

alert(names)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.3.0/lodash.min.js"></script>


For the record, this is how you can do in pure JS:

const characters = [
  { 'name': 'barney',  'age': 36, 'blocked': false },
  { 'name': 'fred',    'age': 40, 'blocked': true  },
  { 'name': 'pebbles', 'age': 1,  'blocked': false },
]

const names = characters
  .filter(c => c.age < 40)
  .map(c => c.name)

alert(names)

4
  • it works, i have used find over filter is because filter loop through all values and then return matched array. thanks for helping me out
    – Salman
    Aug 30, 2014 at 11:02
  • 4
    pluck isn't in lodash anymore :(
    – SSH This
    Feb 9, 2016 at 16:24
  • 1
    I updated the answer to match this API update, actually pluck is simply a map. This is actually even clearer with the ES6 arrow functions. Feb 10, 2016 at 14:06
  • _.map also supports the _.property iteratee shorthand like _.map(characters, 'name') Mar 16, 2016 at 14:16
7
_.result(_.find(characters, function(obj) {
       return obj.age === 40;
}), 'name');
7

_.property

var array = [{a: 1, b: 2}, {a: 3, b: 4}]
array.map(_.property('a')) // => [1, 3]

_.map short hand

var array = [{a: 1, b: 2}, {a: 3, b: 4}]
_.map(array, 'a') // => [1, 3]
0
0

As elclanrs mentioned in comment before obvious solution is just to access the property age after you filtered the object.

But if you wanted to be done in method, you can first extract all age values and then allpy find function on them:

var ageValues = _.pluck(characters, 'age'); //returns [36, 40, 1]

var resultAgeValue = _.find(ageValues, function(ageValue) {
   return  ageValue < 40
});

or, better looks in chain:

var resultAgeValue = _(characters).pluck('age').find(function(ageValue) {
   return  ageValue < 40
});

try jsFiddle: http://jsfiddle.net/pqr/j8rL780u/

3
  • Your fiddle doesn't work, it only returns the first matching character. Aug 27, 2014 at 14:57
  • Actually my jsfiddle returns the first matching character by desing! The original code in the question also returns only first matching object - author was using find, not filter!
    – Petr
    Aug 27, 2014 at 15:07
  • The author appears to begin with Lodash. It doesn't make much sense to return only one value when considering a non-sorted array. Hence my use of filter. Aug 27, 2014 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.