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

nested-javascript-search

v1.0.16

Published

Javascript Array deep search filter.

Downloads

10

Readme

nested-javascript-search

This searcher checks the javascript object elements of an array and search for string match, even in nested arrays inside javascript objects, and retreieves the original array elements that matches with the string parameter.

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@latest/dist/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/index.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.


const searcher = require('../dist/index.js')

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

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

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

console.log(searcher.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.

Blacklist

You can also set a blacklist for searching. The blacklist must be an array of strings. Blacklisted elements will be ignored from the keys of the javascript objects during the search.


const searcher = require('../dist/index.js')

const myArray = [
    {
        id:0,
        name:'foo',
        lastname: 'bar'
        
    },
    {
        id:1,
        name:'bar',
        lastname: 'footer'
    },
]

const blacklist = [
    'name'
]

// It wont check keys called "example_name", so it will retrieve
// the object that its property "lastame" is "footer",
// since "footer" matches with "foo".
console.log(search(myArray,'foo', blacklist))      // [ { id: 1, example_name: 'bar', lastname: 'footer' } ]

This also works on nested properties keys.

Author

Gabriel de Jesús Chirinos Marcano