How can I get the first n (for example 3) elements of an object with underscore?
Thank you!
How can I get the first n (for example 3) elements of an object with underscore?
Thank you!
http://underscorejs.org/#first
_.first allows you to pass a number to get the first n elements of an array.
_.first([1,2,3], 2) // [1,2]
If by object, you mean associative object, the values are not in any specified order. You could do
_.first(_.values( { 'a' : 5, 'b' : 6, 'c' : 8 }), 2) // [5,6]
but it is not guaranteed that the values you get out will be the 'first' ones, so I'm not sure when that would be useful.