javascript - 如何获取lodash链中的集合?

标签 javascript 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()

关于javascript - 如何获取lodash链中的集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45304764/

相关文章:

javascript - 动态地将属性/功能添加到服务/工厂

javascript - 未捕获的类型错误 : Cannot read property 'style' of null at HTMLButtonElement. <匿名>

javascript - 如何在 Vue.js 单文件组件中等待 Facebook SDK 加载

javascript - 从剪贴板获取粘贴的图像 Firefox

javascript - 嵌套对象迭代并返回空值键名nodejs

javascript - NodeJS : Chain functions automatically in a promise?

ssl - Kafka,为什么在使用 kafka-console- Producer (或 Kafkcat )进行测试时需要传递证书链而不仅仅是根 ca

javascript - Promise:忽略捕获并返回链

javascript - 使用 Lodash 拔取嵌套对象的键

javascript - 合并和分组两个Javascript对象数组和分组