如何使用下划线js返回自定义组?

时间:2018-01-23 12:58:42

标签: javascript jquery underscore.js underscore.js-templating

我使用rest api调用和绑定数据,并尝试使用下划线js按功能管理。但我无法确定如何做到这一点。

这是我返回的数据图片: enter image description here

我正在通过on int值进行分组工作正常但我的预期输出是0-1,1-2,2-3分组distance值 假设数组返回距离0,0.224,0.50,1.22 然后输出应该是两个数组0: 0,0.224,0.501: 1.22

感谢。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你应该使用

_.groupBy(data.response.data, function(d) { return Math.floor(d.distance); })

在这种情况下,您将获得您期望的输出。 查看演示:

var array = [0,0.224,0.50,1.22]

console.log(_.groupBy(array, Math.floor));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

相关问题