I have an object like so:
> Object
> Rett@site.com: Array[100]
> pel4@gmail.com: Array[4]
> 0
id : 132
selected : true
> 1
id : 51
selected : false
etc..
How can I use the underscore _.filter()
to return back only the items where selected === true?
I've never had the need to go down to layers with _.filter()
. Something like
var stuff = _.filter(me.collections, function(item) {
return item[0].selected === true;
});
Thank you
If you want to pull all array elements from any e-mail address where selected
is true
, you can iterate like so:
var selected = [];
for (email in emailLists) {
selected.concat(_.filter(emailLists[email], function (item) {
return item.selected === true;
}));
}
If you only want to pull the arrays where all elements are selected
, you might instead do something like this:
var stuff = _.filter(me.collections, function(item) {
return _.all(item, function (jtem) {
jtem.selected === true;
});
});
Underscore's filter method will work on an object being used as a hash or dictionary, but it will return an array of the object's enumerable values and strip out the keys. I needed a function to filter a hash by its values that would preserve the keys, and wrote this in Coffeescript:
hash_filter: (hash, test_function) ->
keys = Object.keys hash
filtered = {}
for key in keys
filtered[key] = hash[key] if test_function hash[key]
filtered
If you're not using Coffeescript, here's the compiled result in Javascript, cleaned up a little:
hash_filter = function(hash, test_function) {
var filtered, key, keys, i;
keys = Object.keys(hash);
filtered = {};
for (i = 0; i < keys.length; i++) {
key = keys[i];
if (test_function(hash[key])) {
filtered[key] = hash[key];
}
}
return filtered;
}
hash = {a: 1, b: 2, c: 3};
console.log((hash_filter(hash, function(item){return item > 1;})));
// Object {b=2, c=3}
TL; DR: Object.keys() is great!
I have an object called allFilterValues containing the following:
{"originDivision":"GFC","originSubdivision":"","destinationDivision":"","destinationSubdivision":""}
This is ugly but you asked for an underscore based way to filter an object. This is how I returned only the filter elements that had non-falsy values; you can switch the return statement of the filter to whatever you need:
var nonEmptyFilters = _.pick.apply({}, [allFilterValues].concat(_.filter(_.keys(allFilterValues), function(key) {
return allFilterValues[key];
})));
Output (JSON/stringified):
{"originDivision":"GFC"}
Maybe you want a simplest way
_.filter(me.collections, { selected: true})