Javascript Underscore.js _.map 函数:跳过一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30804447/
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-08-23 05:46:19  来源:igfitidea点击:

Underscore.js _.map function : skip a value

javascriptunderscore.js

提问by foucdeg

I am trying to use underscore.js _.mapfunction on an array of objects, to get an array with a property of each object. That's the usual scenario, so :

我试图_.map在对象数组上使用 underscore.js函数,以获取具有每个对象属性的数组。这是通常的情况,所以:

var finalArray = _.map(myArray, function(obj) {
    return obj.myProperty;
});

But in some cases I need that nothingbe added in the array. It could be something like :

但在某些情况下,我不需要在数组中添加任何内容。它可能是这样的:

var finalArray = _.map(myArray, function(obj) {
    if (!obj.ignore) {
        return obj.myProperty;
    }
});

The result of this is that an undefinedvalue is pushed into the array, which is not the same as not pushing anything at all.

这样做的结果是一个undefined值被推送到数组中,这与根本不推送任何内容是不同的。

Is there a way for the map function not to push a value, or do I need to post-process my finalArrayto remove the unwanted undefined's?

有没有办法让 map 函数不推送一个值,或者我是否需要对 myfinalArray进行后处理以删除不需要的undefined's ?

采纳答案by couettos

you should use _.filter() before _.map()

你应该在 _.map() 之前使用 _.filter()

var filteredArray = _.filter(myArray,function(obj) {
     return !obj.ignore;
});

var finalArray = _.map(filteredArray, function(obj) {
    return obj.myProperty;
});

回答by Thomas Eschemann

You could use reduce:

您可以使用减少:

myArray.reduce(function (acc, obj) {
    if (!obj.ignore) {
        acc.push(obj.myProperty);
    }
    return acc;
}, []);

or with lodash:

或使用 lodash:

_.reduce(myArray, function (acc, obj) {
  if (!obj.ignore) {
    acc.push(obj.myProperty);
  }
  return acc;
}, []);

回答by Pavlo

This can be done using Underscore methods alone, points-free style:

这可以单独使用 Underscore 方法完成,无点样式

var finalArray = _.chain(myArray)
    .reject('ignore')
    .pluck('myProperty')
    .value();

回答by andlrc

.mapwill map out a new value to the array, you can use .filterto filter the array. FYI you can use .pluckto map a property from an object:

.map将映射出一个新值到数组,你可以.filter用来过滤数组。仅供参考,您可以使用.pluck从对象映射属性:

_.chain(myArray).filter(function(obj) { return !obj.ignore; }).pluck('myProperty').value();

You can also use .wherelike this:

你也可以.where这样使用:

_.chain(myArray).where({ignore: false}).pluck('myProperty').value();

Or:

或者:

_.pluck(_.where(myArray, {ignore: false}), 'myProperty');

回答by Michael R

Try using lodash's compact(). It creates an array with all falsey values removed. Thus, the values false, null, 0, "", undefined, and NaNare removed.

尝试使用 lodash 的compact(). 它创建一个数组,其中删除了所有 falsey 值。因此,该值falsenull0""undefined,和NaN被去除。

https://lodash.com/docs#compact

https://lodash.com/docs#compact

I'm using it—it's very clean.

我正在使用它——它非常干净。

回答by TaoPR

So this means you want to filter, then map your array.

所以这意味着您要过滤,然后映射您的数组。

To do this in underscoreway, you will need to pass your array into this pipeline:

要以这种underscore方式执行此操作,您需要将数组传递到此管道中:

myArray --> filter--> map--> finalArray

myArray -->过滤器-->映射--> finalArray

var isIncluded = function(obj){
    return !obj.ignore
}
var takeProperty = function(obj){
    return obj.myProperty
}
var finalArray = _.map( _.filter(myArray, isIncluded), takeProperty  );

回答by Kevin Le - Khnle

Here's another way. This one makes use of the compose function in underscore. Why compose? compose is backed by Category theory in Mathematics. This way is also point-free.

这是另一种方式。这个在下划线中使用了 compose 函数。为什么要作曲?compose 得到了数学范畴论的支持。这种方式也是免积分的。

var pluckMyProp = function(array) {
    return _.pluck(array, 'myProperty')
}

var reject = function(array) {
    return _.reject(array, function(element) {
        return element['ignore'] === true
    })
}

var finalArray = _.compose(pluckMyProp, reject)
var data = [{myProperty: 1, ignore: false}, {ignore: true}, {myProperty: 2}]


finalArray(data) //=> [1,2]