如何使用下划线获取对象的前 n 个元素? [英] How to get first n elements of an object with underscore?
本文介绍了如何使用下划线获取对象的前 n 个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取带有下划线的对象的前 n 个(例如 3 个)元素?
谢谢!
解决方案
http://underscorejs.org/#first>
_.first 允许您传递一个数字来获取数组的前 n 个元素.
_.first([1,2,3], 2)//[1,2]
如果按对象是指关联对象,则值没有任何指定的顺序.你可以这样做
_.first(_.values( { 'a' : 5, 'b' : 6, 'c' : 8 }), 2)//[5,6]
但不能保证您得到的值将是第一个"值,所以我不确定什么时候会有用.
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.
这篇关于如何使用下划线获取对象的前 n 个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文