How to get first n elements of an object with underscore?

只谈情不闲聊 提交于 2019-12-06 18:42:54

问题


How can I get the first n (for example 3) elements of an object with underscore?

Thank you!


回答1:


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.



来源:https://stackoverflow.com/questions/26127996/how-to-get-first-n-elements-of-an-object-with-underscore

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!