骨干集合:抓取集合的不同值 [英] Backbone collection: Retrieve distinct values of a collection
问题描述
我的模型骨干收集和想检索某个属性的不同值
I have backbone collection of models and would like to retrieve the distinct values of a certain property
如果我已加载数据,如以下到我的收藏:
If I have loaded data like the following into my collection:
[{brand:'audi',id:'1234'},
{brand:'audi',id:'3456'},
{brand:'bmw',id:'3456'}]
现在我想从收集的独特品牌。结果应该是:
I would now want to get the distinct brands from the collection. The result should be:
['audi','bmw']
纵观骨干,并强调API我看不出有什么明显的实现这一目标。
我目前的做法是使用像 jslinq
我缺少什么明显的还是有人比jslinq一个更好的建议?
Am I missing anything obvious or does somebody have a better suggestion than jslinq?
推荐答案
您可以使用勇气,然后使用的uniq (例如: http://jsfiddle.net/sCVyN/5 / )
You can use pluck and then use uniq (example: http://jsfiddle.net/sCVyN/5/)
勇气
什么是可能是最常见的用例的一个方便的版本
图:提取属性值的列表
A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.
uniq的
生成阵列的无重复的版本,使用 ===
测试
反对平等。如果事先该数组排序知道,
经过如此isSorted将运行更快的算法。如果你
要计算根据一个变换的唯一项目,传递
迭代器功能。
Produces a duplicate-free version of the array, using
===
to test object equality. If you know in advance that the array is sorted, passing true for isSorted will run a much faster algorithm. If you want to compute unique items based on a transformation, pass an iterator function.
这篇关于骨干集合:抓取集合的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!