magic-object-search
v1.0.2
Published
๐ Easy search among objects
Downloads
5
Maintainers
Readme
โจ Magic Object Search
magic-object-search
is a lightweight utility library published on npm, designed to simplify searching through arrays of objects in TypeScript or JavaScript environments. It provides a simple yet powerful function to search for a specific string within specified properties of objects in an array.
Installation
You can install magic-object-search
via npm:
npm install magic-object-search
Usage
To use magic-object-search
, import the magicObjectSearch function and pass in the search string, array of objects, and properties to search within.
import { magicObjectSearch } from 'magic-object-search';
interface MyObject {
id: number;
name: string;
description: string;
}
const objects: MyObject[] = [
{ id: 1, name: '๐ Apple', description: 'A fruit with red or green skin' },
{ id: 2, name: '๐ Banana', description: 'A long curved fruit that grows in clusters' },
{ id: 3, name: '๐ Orange', description: 'A round citrus fruit with a tough shiny orange skin' }
];
const searchResults = magicObjectSearch('fruit', objects, ['name', 'description']);
console.log(searchResults);
/* Output:
[
{
id: 1,
name: '๐ Apple',
description: 'A fruit with red or green skin'
},
{
id: 2,
name: '๐ Banana',
description: 'A long curved fruit that grows in clusters'
}
]
*/
API
magicObjectSearch<T>(searchValue: string | number | boolean | null | undefined, options: T[], properties: (keyof T)[]): T[]
searchValue
(string | number | boolean | null | undefined): The value to search for within the specified properties of objects.options
(T[]): An array of objects to search within.properties
(keyof T)[]: An array of property names (keys) within objects to search for the given string.
Contributions
Contributions are welcome! Feel free to submit issues or pull requests.
License
This project is licensed under the MIT License. See the LICENSE file for details.