javascript Lodash - 从 Map 方法有条件地返回对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32539480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:26:32  来源:igfitidea点击:

Lodash - Conditionally Return Object from a Map Method

javascriptlodash

提问by musubi

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 trueor false.

我想遍历一个数组,运行一个计算,如果结果的条件为真,则返回一个新对象。 _.filter(...)在这里不起作用,因为迭代器函数必须返回truefalse

_.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.

我试过到处搜索,包括文档,但我还没有找到一种很好的方法。

回答by 7zark7

Sounds like maybe you want reduceand not map:

听起来也许你想要reduce而不是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:

此外,如果您是 ES6+ 和/或使用 Babel,这可能是列表推导式的一个很好的用途:

let newArray = [for (person of people)
                if (calculateAge(person.birthDate) > 50)
                { person: person, age: calculateAge(person.birthDate) }
               ];
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:

更新:列表推导式已从 Babel 6 中删除。 ES2015 版本将如下所示:

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)

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

回答by Chrillewoodz

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.

这将首先过滤列表,然后返回一个包含年龄在 50 岁以上的人的对象的新数组。

回答by Max Brodin

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

见工作小提琴