Giter VIP home page Giter VIP logo

filter-by-criteria's Introduction

Filter By Criteria

This utility function is used to filter an array of json objects based on a criteria object. It was developed to be a generic filter that can be used on data produced from any API.

It requires an Object with criteria, and the Array of data objects.

// Example Criteria object
criteria: {
  name: 'Kevin',
  department: 'Business Systems'
}
// Example Data object
data: [{
  name: 'Bob',
  department: 'Programming'
}, {
  name: 'Kate',
  department: 'Finance'
}, {
  name: 'Kevin',
  department: 'Business Systems'
}]

// Using the filter, an array of just one object with the name of 'Kevin' and department of 'Business Systems' will be returned.

IMPORTANT
The keys of every propery within the Criteria, must have a matching key property within the data object. There will be warnings in the console if you do not conform to this.

import filterByCriteria from 'filter-by-criteria'

function filteredData (criteria, dataArray) {
  return filterByCriteria(criteria, dataArray)
}

Advanced filtering

The default filtering should be sufficient for most cases, however if a certain property requires advanced filtering, there is the option to provide a custom function to the filter for that property key.

customFilter must be an Array of Objects with the following properties:

{
  key: String,
  filter: Function (criteria, item)
}

Example:

function filteredData (criteria, dataArray) {
  // Array of filter functions - defined with a property key that the filter applies to
  const customFilter = [{
    key: 'department',
    filter: function (criteria, item) {
      // Code your custom filter function here
      // Must return Boolean
      return criteria == item
    }
  }]
  return filterByCriteria(criteria, items, customFilter)
}

The above custom filter function will override the default filter function when filtering the 'department' property of all objects within the data array.

filter-by-criteria's People

Contributors

keha94 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.