Skip to content

Suggestion/Patch: filterByKeys - filter array by multiple keys #1094

Closed
@borgand

Description

@borgand

A common requirement for a filterBy search in ... is to specify multiple keys to look, but not all of them (as that might produce false positives from fields irrelevant/invisible to the user).

Below is a crude modification of filterBy, dubbed filterByKeys. Usage example:

<li v-repeat="users | filterByKeys searchText in 'firstname' 'lastname'">{{username}}</li>

Would it be possible to add this filter to the core (or perhaps replace filterBy with it, as it should be backwards compatible)?

Currently, keeping this in a separate custom filters file requires duplicating the contains function and pulling in the same requirements as for array-filters.js itself.

var filterByKeys = function(){
    // Convert arguments to an actual Array
    var args = [].slice.call(arguments)
    var arr = args.shift()
    var search = args.shift()
    // cast to lowercase string
    search = ('' + search).toLowerCase()
    // strip optional 'in'
    if (args[0] == 'in'){
      delete args[0]
    }
    // if left with 0 keys, fall back to all keys
    var keys = args.length > 0 ? args : Object.keys(args);

    // filter input array
    return arr.filter(function (item) {
      var result = false
      keys.forEach(function(datakey){
        result = result || contains(Path.get(item, datakey), search)
      })
      return result;
    })
  }

PS. Possible issues might arise from the arguments conversion as that is a bit funky and I'm not 100% sure of browser compatibility

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions