我目前正在使用带有链的 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()
关于javascript - 如何获取lodash链中的集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45304764/