You could do something like this to get your id/name pairs in the desired order:
var map_id_to_name = function(m, o) {
m[o.franchisee_id] = o.store_name;
return m;
};
var arrayify = function(name, id) {
return [ name, id ];
};
var stores = _(data).chain()
.reduce(map_id_to_name, { })
.map(arrayify)
.sortBy(_.first)
.value();
Demo: http://jsfiddle.net/ambiguous/9xxS6/
That will give you an array of arrays in stores
that you can spin through to build your <option>
s; stores
will look like this:
[
[ "Store 1", "id01" ],
[ "Store 2", "id02" ],
...
]
The store name will be in the first entry of the inner arrays and the franchise ID in the second. The whole thing will be sorted by store name. If you want a case insensitive sort then you can .sortBy(function(a) { return a[0].toLowerCase() })
instead.
The reduce(map_id_to_name, { })
collects the unique id/name pairs using an Object to automatically enforce uniqueness (the keys in an Object are unique after all). Then map(arrayify)
converts the Object to an array-of-arrays so that you can sort things. You could use an array-of-objects instead, that would just be a small alteration to the map
and sortBy
calls.