5

I want to iterate through an array, run a calculation, and if the condition is true for the result, return a new object. _.filter(...) would not work here, since the iterator function must return either true or false.

_.map(people, function(person) {

    var age = calculateAge(person.birthDate);

    if (age > 50) {
        return {person: person, age: age};
    }

});

I've tried searching all over, including the documentation, but I haven't found a way to do this well.

0

3 Answers 3

10

Sounds like maybe you want reduce and not map:

var newArray = _.reduce(people, function(results, person) {
  var age = calculateAge(person.birthDate);
  if (age > 50) {
     results.push({ person: person, age: age });
  }
  return results;
}, []);

Also if you are ES6+ and/or using Babel, this might be a good use for list comprehensions:

let newArray = [for (person of people)
                if (calculateAge(person.birthDate) > 50)
                { person: person, age: calculateAge(person.birthDate) }
               ];

Update: List comprehensions have been dropped from from Babel 6. The ES2015 version would look like:

const newArray = people.reduce((results, person) => {
  const age = calculateAge(person.birthDate);
  return (age > 50) ? [...results, { person, age }] : results;
}, []);

(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator)

1
  • In both your examples, where does the variable age come from?
    – user663031
    Sep 12, 2015 at 14:35
0

This could work for you:

var sorted = _.filter(people, function(person) {

    var age = calculateAge(person.birthDate);

    if (age > 50) {
      return true;
    }
});

var newArray = _.map(sorted, function(person) {

   var age = calculateAge(person.birthDate);

   return {person: person, age: age};
});

This will first filter the list and then return a new array with the objects of the people whos age is above 50.

2
  • 1
    The variable age is not defined in the callback to map. Also, why would you need to do another check on age if you've already filtered the youngsters out? Also, return age > 50; is more idiomatic. To avoid recalculating age, you could stick it on the object in the filter, That would also allow you to use pick to return from the map.
    – user663031
    Sep 12, 2015 at 13:56
  • I had the same issue as what @torazaburo mentioned. Ideally I would want to make a database call instead of the calculateAge(...) method, so I would want to avoid querying the DB twice.
    – musubi
    Sep 12, 2015 at 13:58
0

Try this:

people = _.pluck(
                _.filter(
                        _.map(people, function(person) {
                            return { person: person, age: calculateAge(person.birthDate) };
                        }), 
                        function(person) {
                            return person.age > 50;
                        }
                ), 
                "person"
        );

See working fiddle

1
  • You can chain methods instead of nesting them in lodash : ))
    – moonwave99
    Sep 12, 2015 at 16:00

Your Answer

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

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