2016-01-06 71 views
2

我在数组的所有元素上做了_.reduce,不幸的是有时候这个数组太大了。如何仅在数组的第一个元素上使用_.reduce

我想减少数组中的常量元素。

你建议我做什么?

+0

您可以使用['_.slice'(https://lodash.com/docs#slice) –

+0

@hege_hegedus这是“lodash”,而不是“下划线”... – Alnitak

回答

3

您可以使用Array.prototype.slice,使阵列的第一n元素的(临时)副本:

_.reduce(myArray.slice(0, n), ...); 

如果有数组这将只用他们都在不到n元素。

1

如何使用_.first得到要减少项目的数量:

// reduce the first 100 items in the array 
var result = _.reduce(_.first(data, 100), fn, memo) 
相关问题