2014-01-14 67 views

回答

3

你可以使用_.times

_.times(n, iterator, [context])

調用給定迭代函數ñ倍。 迭代器的每個調用都被稱爲索引參數。生成返回值的數組。

所以:

var things = _(10).times(function(i) { return { id: i + 1 } }); 

,或者如果你需要他們以相同的順序爲您for循環:

var things = _(10).times(function(i) { return { id: i + 1 } }) 
        .reverse(); 

演示:http://jsfiddle.net/ambiguous/arZB8/

+0

+1。 '_(10)'做什麼?譁衆取寵? – thefourtheye

+0

@thefourtheye'_.f(x)'和'_(x).f()'做同樣的事情,我認爲後者更漂亮,讀得更好。 –

+0

但它是如何做到的? '_'基本上是一個對象,那麼'_(x)'是如何工作的呢? – thefourtheye

相關問題