Giter VIP home page Giter VIP logo

smart-array-filter's Introduction

smart-array-filter

NPM version build status Test coverage npm download

Filter an array of objects

Installation

npm install smart-array-filter

Documentation

Example

const array = [
  { id: 'A', pet: 'dog' },
  { id: 'B', pet: 'cat' },
];
const filteredData = filter(array, {
  limit: 1,
  keywords: 'Do', // search for any field that contains the "Do" string
  caseSensitive: true,
});
var array = [
  { id: '1', pet: 'dog' },
  { id: '2', pet: 'cat' },
  { id: '3', pet: 'horse' },
];
filter(array, {
  keywords: '-pet:cat', // not a cat
});

filter(array, {
  keywords: '2', // any field has the value 2
});

filter(array, {
  keywords: '>2', // any field has the value after 2. Strings will be included and letters are after numbers in ascii code
});

filter(array, {
  keywords: 'id:>=2', // id greater or equal to 2
});

filter(array, {
  keywords: 'id:1..2', // id between 1 and 2 (including 1 and 2)
});

If you enter many criteria by default there is a 'AND' combination

var array = [{ animals: ['dog', 'cat'] }, { animals: ['horse', 'cat'] }];
filter(array, {
  keywords: 'animals:cat', // array must contain a cat
});
filter(array, {
  keywords: 'animals:o', // by default it is include so match dog and horse
});
filter(array, {
  keywords: 'animals:=o', // must contain exactly a 'o'
});
var array = [{ a: [{ b: 1 }, { c: 2 }] }, { a: [{ b: 2 }, { d: 2 }] }];
filter(array, {
  keywords: 'a.b:>=1', // 2 consecutive properties (not including array indices)
});
filter(array, {
  keywords: 'dog:>=1',
  pathAlias: { dog: 'a.b' }, // you can define path alias
});
filter(array, {
  keywords: 'b:>1', // 2 consecutive properties (not including array indices)
});
filter(array, {
  keywords: 'd:2', // 2 consecutive properties (not including array indices)
});

License

MIT

smart-array-filter's People

Contributors

targos avatar lpatiny avatar stropitek avatar cheminfo-bot avatar wadjih-bencheikh18 avatar

Watchers

 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.