如何仅对数组的某些第一个元素使用 _.reduce
How to use _.reduce only on some first elements of the array
我对数组的所有元素都进行了 _.reduce,不幸的是有时数组太大了。
我只想减少数组中固定数量的元素。
你建议我做什么?
您可以使用 Array.prototype.slice
制作数组前 n
个元素的(临时)副本:
_.reduce(myArray.slice(0, n), ...);
如果数组中的元素少于 n
个,它将只使用所有元素。
如何使用 _.first 获取要减少的项目数:
// reduce the first 100 items in the array
var result = _.reduce( _.first(data, 100), fn, memo)
我对数组的所有元素都进行了 _.reduce,不幸的是有时数组太大了。
我只想减少数组中固定数量的元素。
你建议我做什么?
您可以使用 Array.prototype.slice
制作数组前 n
个元素的(临时)副本:
_.reduce(myArray.slice(0, n), ...);
如果数组中的元素少于 n
个,它将只使用所有元素。
如何使用 _.first 获取要减少的项目数:
// reduce the first 100 items in the array
var result = _.reduce( _.first(data, 100), fn, memo)