简体   繁体   中英

How to use Underscore.js filter with an object?

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"}

@Dexygen was right to utilize _.pick but a cleaner solution is possible because the function also accepts a predicate

Return a copy of the object, filtered to only have values for the allowed keys (or array of valid keys). Alternatively accepts a predicate indicating which keys to pick .

(highlight is mine)

Here's a real life example I've used in a project

_.pick({red: false, yellow: true, green: true}, function(value, key, object) {
    return value === true;
});
// {yellow: true, green: true}
const obj = {
    1 : { active: true },
    2 : { active: false },
    3 : { active: false },
}

let filtered = Object.entries(obj).reduce((acc, current) => {
    const currentEntry = current[1];
    const currentKey = current[0];
    //here you check condition
    if (currentEntry.active) {
      return {
        ...acc,
        [currentKey]: currentEntry
      }
    }
    return acc;
}, {})

There is a rule of thumb, if you need to achieve something really exotic look up into reducer it can solve almost all problems related to objects, it's a bit tricky to get used to it, but trust me thorough reading of documentation gonna pay off.

也许你想要一个最简单的方法

_.filter(me.collections, { selected: true})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM