如何在 lodash 链中获取集合?

How to get collection in a lodash chain?

我目前正在使用带链的 lodash。我做了类似的事情:

let result = _(myCollection)
    .filter(...)
    .map(...)
    .value()
return _.reduce(result, (a, b) => a && b, result.length != 0)

但我对此并不满意。我想在一条指令中完成所有操作以获得类似的东西:

return _(myCollection)
    .filter(...)
    .map(...)
    .reduce((a, b) => a && b, myMappedCollection.length != 0)
    .value()

我找不到恢复当前处理的集合的方法。有办法吗?

如文档中所述:https://lodash.com/docs/4.17.4#reduce

Reduces collection to a value which is the accumulated result of running each element in collection thru iteratee, where each successive invocation is supplied the return value of the previous. If accumulator is not given, the first element of collection is used as the initial value. The iteratee is invoked with four arguments: (accumulator, value, index|key, collection).

您可以执行以下操作

return _(myCollection)
.filter(...)
.map(...)
.reduce((a, b, indx, myMappedCollection) => a && b, myMappedCollection.length != 0)
.value()