nested-javascript-search

1.0.10 • Public • Published

nested-javascript-search

This searcher checks the elements of an array and search for string match.

Installation

# Using npm
npm i nested-javascript-search

Usage

At first import the library to your project.

import { search } from 'nested-javascript-search'

// or by using a CDN.

import { search } from 'https://unpkg.com/nested-javascript-search@1.0.0/src/index.js'

You can also use it in your html using a script tag with "module" as type.

<script type="module">
    import search from 'https://unpkg.com/nested-javascript-search@latest/dist/nested-javascript-search.min.js'
    
    // Then you can use search as a function here.
</script>

Then you can call to the search function.

search(myArray, myStringToMatch)

Example

Filter the array elements by searching into the nested array. It will then return the main array element due to the nested string match.

import { search } from 'nested-javascript-search'

const myArray = [
    {
        id:0,
        name:'foo',
        nested: {
            name:'cake'
        }
    },
    {
        id:1,
        name:'bar',
        nestedArray:[
            {name:'coffee'}
        ]
    },
]

console.log(search(myArray,'coffee'))   // [ { id: 1, name: 'bar', nestedArray: [ [Object] ] } ]

console.log(search(myArray,'foo'))      // [ { id: 0, name: 'foo', nested: { name: 'cake' } } ]

console.log(search(myArray,'cake'))      // [] This version only search inside arrays of objects.
 

This searcher only works with arrays, so it wont work with values as nested objects.

Author

Gabriel de Jesús Chirinos Marcano

Package Sidebar

Install

npm i nested-javascript-search

Weekly Downloads

1

Version

1.0.10

License

ISC

Unpacked Size

6.59 kB

Total Files

4

Last publish

Collaborators

  • gachimar