npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

filter-data-advanced

v1.0.8

Published

filters any type of json data/array of objects in typescript/javascript

Downloads

60

Readme

Filter Data Advanced

This package is useful to filter any kind of JSON data (or) array of objects using javascript/typescript.
All methods will work for nested array of objects as well and data filter is case insensitive.

Installation

npm i filter-data-advanced


Methods

  1. filterByKeyValue(data: any[], key: string, value: string)
  2. filterByValue(data: any[], value: string)
  3. filterByKeyAndMultiValues(data: any[], key: string, values: string[])
  4. filterByNestedKeyValues(data: any[], mainKey: string, innerKey: string, values: string[])
  5. filterDataBetweenNumbers(data: any[], key: string, startNumber: number, endNumber: number)
  6. filterDataBetweenDates(data: any[], key: string, startDate: Date, endDate: Date, isEndDateInclusive:boolean)
  7. filterDataOnSingleDate(data: any[], key: string, exactSingleDate: Date)

Usage

Below are the examples and methods usage to explain the search/filter of data.
(NOTE: Whenever a string value is used to search, LIKE search is performed. If you want EXACT search, provide the exact full word/string. This will make LIKE search = EXACT search in 95% of cases.)

import{FilterDataAdvanced} from 'filter-data-advanced/dist/FilterDataAdvanced';

const myData = [
  { name: "John", age: 25, city: "New York" },
  { name: "Sarah", age: 30, city: "Los Angeles" },
  { name: "Tom", age: 35, city: "San Francisco" },
  { name: "Jane", age: 22, city: "Chicago" },
  { name: "Bob", age: 22, city: "San Francisco" }
];

let obj = new FilterDataAdvanced();
//Currently dependency injection is not available in order to make this package available for Typescript, Angular and React.

Method 1: filterByKeyValue(data: any[], key: string, value: string)

This method takes an array of objects, a key name, and a value to be searched. The search is case-insensitive.

let result1 = obj.filterByKeyValue(myData, "city", "San Fra");
let result11 = obj.filterByKeyValue(myData, "city", "San Francisco");

console.log(result1); 
console.log(result11);

//Both will return same output
//{ name: 'Tom', age: 35, city: 'San Francisco' },
//{ name: 'Bob', age: 22, city: 'San Francisco' }

Method 2: filterByValue(data: any[], value: string)

This method takes an array of objects and a value to be searched. It searches all the keys of the object and returns the objects that contain the search value.

let result2 = obj.filterByValue(myData, "bob");
console.log(result2);
//[ { name: 'Bob', age: 22, city: 'San Francisco' } ]

Method 3: filterByKeyAndMultiValues(data: any[], key: string, values: string[])

This method takes an array of objects, a key name, and an array of values to be searched. It returns the objects that contain any of the search values in the specified key.

let result3 = obj.filterByKeyAndMultiValues(myData, "name", ["tom","Bob"]);
console.log(result3);
// { name: 'Tom', age: 35, city: 'San Francisco' },
// { name: 'Bob', age: 22, city: 'San Francisco' }

Method 4: filterByNestedKeyValues(data: any[], mainKey: string, innerKey: string, values: string[])

This method takes an array of objects, a main key, an inner key, and an array of values to be searched. It searches for the search values in the specified inner key of the objects that are nested within the main key of the parent object.

const myData1 = [
  { name: "John", age: 25, city: "New York",
  booksData:[{title:"ABC",publisher:"PAN Books"},{title:"XYZ",publisher:"JK Sons publishing"}]},
  { name: "Sarah", age: 30, city: "Los Angeles",
  booksData:[{title:"The Hero",publisher:"PAN Books"},{title:"The Villain",publisher:"JK Sons publishing"}] },
  { name: "Tom", age: 35, city: "San Francisco",
  booksData:[{title:"The King",publisher:"PAN Books"},{title:"The Queen",publisher:"JK Sons publishing"}] },
];

let result4 = obj.filterByNestedKeyValues(myData1, "booksData","title", ["the hero","abc"]);
console.log(result4);
//{ name: "John", age: 25, city: "New York",booksData:[{title:"ABC",publisher:"PAN Books"},{title:"XYZ",publisher:"JK Sons publishing"}]},
 //{ name: "Sarah", age: 30, city: "Los Angeles",booksData:[{title:"The Hero",publisher:"PAN Books"},{title:"The Villain",publisher:"JK Sons publishing"}] },

Method 5: filterDataBetweenNumbers(data: any[], key: string, startNumber: number, endNumber: number)

This method takes an array of objects, a key name, a start number, and an end number. It returns the objects that have a number value within the specified range.

let result5 = obj.filterDataBetweenNumbers(myData,"age", 25,35);
console.log(result5);

//{ name: 'John', age: 25, city: 'New York' },
//{ name: 'Sarah', age: 30, city: 'Los Angeles' },
//{ name: 'Tom', age: 35, city: 'San Francisco' }

Method 6: filterDataBetweenDates(data: any[], key: string, startDate: Date, endDate: Date, isEndDateInclusive:boolean)

This method takes an array of objects, a date key, a start date, an end date, and a flag indicating whether the end date should be included in the results. It returns the objects that have a date within the specified date range.

const myData = [
  { name: "John", age: 25, city: "New York", recordUpdatedOn:"2023-03-01 14:17:59.150"},
  { name: "Sarah", age: 30, city: "Los Angeles",recordUpdatedOn:"2023-03-02 02:15:59.150"},
  { name: "Tom", age: 35, city: "San Francisco",recordUpdatedOn:"2023-03-03 11:25:59.150"},
  { name: "Jane", age: 22, city: "Chicago",recordUpdatedOn:"2023-03-04 10:31:59.150" },
];

let result6 = obj.filterDataBetweenDates(myData,"recordUpdatedOn", new Date("2023-03-01 00:00:00"), new Date("2023-03-02 00:00:00"), true);
console.log(result6);

//{ name: "John", age: 25, city: "New York", recordUpdatedOn:"2023-03-01 14:17:59.150"},
//{ name: "Sarah", age: 30, city: "Los Angeles",recordUpdatedOn:"2023-03-02 02:15:59.150"},

Method 7: filterDataOnSingleDate(data: any[], key: string, exactSingleDate: Date)

This method takes an array of objects, a date key, and a single date. It returns the objects that have the specified date.

let result7 = obj.filterDataOnSingleDate(myData,"recordUpdatedOn", new Date("2023-03-03 00:00:00"));
console.log(result7);
//{ name: "Tom", age: 35, city: "San Francisco",recordUpdatedOn:"2023-03-03 11:25:59.150"},