使用underscore.js可以减少返回数组吗?
我正在尝试使用reduce 返回一个像这样的数组:
var myArray = [1,2,3];
_.reduce(myArray, function (seed, item) { return seed.push(item);}, []);
我希望它会生成一个像myArray 一样的数组。对于第一项,种子是一个数组。那么对于第二项,种子是一个数字。这会导致错误并且永远不会到达第三项。
这里发生了什么事?
I'm trying to use reduce to return an array like so:
var myArray = [1,2,3];
_.reduce(myArray, function (seed, item) { return seed.push(item);}, []);
I expect that it will produce an array just like myArray. Instead for the first item, seed is an array. Then for the second item, seed is a number. That causes an error and the third item is never reached.
Whats happening here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,
seed.push()
不会返回修改后的seed
。执行以下操作,这是正确的:Actually,
seed.push()
does not return the modifiedseed
. Do the following, and it's right: