filter-data-advanced
v1.0.8
Published
filters any type of json data/array of objects in typescript/javascript
Downloads
60
Maintainers
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
- filterByKeyValue(data: any[], key: string, value: string)
- filterByValue(data: any[], value: string)
- filterByKeyAndMultiValues(data: any[], key: string, values: string[])
- filterByNestedKeyValues(data: any[], mainKey: string, innerKey: string, values: string[])
- filterDataBetweenNumbers(data: any[], key: string, startNumber: number, endNumber: number)
- filterDataBetweenDates(data: any[], key: string, startDate: Date, endDate: Date, isEndDateInclusive:boolean)
- 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"},