3 ms·
I think one of the reasons for the third argument (the array itself) is to permit chaining. Consider this: var publishedPosts = posts.filter(function(post)
by ricardobeat 14y ago
I think one of the reasons for the third argument (the array itself) is to permit chaining. Consider this:
var publishedPosts = posts.filter(function(post){
return post.published
})
var intervals = publishedPosts.map(function(post, i){
var next = publishedPosts[i+1]
return post.date - (next && next.date || 0)
})
With the array reference you don't need an intermediate var:
var intervals = posts.filter(function(post){
return post.published
}).map(function(post, i, arr){
var next = arr[i+1]
return post.date - (next && next.date || 0)
})
- krickle 14y agoI think in that case it would be better to make it variadic on input arrays, like map(makeInterval, array, array.tail())