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-object-filter

v1.2.3

Published

This package allows you to filter nested javascript object. It allows you to filter nested object with array of fields. This package has no limit to filter javascript object.

Downloads

3

Readme

Nested-Object-Filter

NPM npm Build Status Coverage Status GitHub top language

This package allows you to filter nested javascript object regardless how nested the object is. You specify an array of nested arrays to filter object based on how deep you want to filter.

Current Version 1.2.3

Version 1.0.0

  • Initial Release

Note!: Reference test file for more samples

Whats new ?

  1. Support Nested Object with nested object paths ref test file
  2. Supports Nested object with simple object paths. ref test file
  3. Has both named and default export for commonJS;

Installation

npm install nested-object-filter

Simple Usage

    const objFilter = require("nested-object-filter");
    const sourceObject = {
        personal: {
            is_business: true,
            business_name: 'Business Name',
            business_description: 'Business description here',
            gender: 'Male',
            city: 'Unknown',
            Region: 'Unknown',
            country: 'Unknown',
            social: {
                whatsapp: 'whatsapp-link-here',
                website: 'website-link',
                facebook: 'facebook-link',
                twitter: 'twitter-link',
                instagram: 'instagram-link',
                statistics: {
                    followers: 1000,
                    following: 62000,
                    likes: 14000,
                    dislikes: 9000
                }
            }
        },
        meta: {
            'last-login': 1574187006717,
            'login-count': 100,
            info: {
                timestamp: 1574178847,
                user_id: 'JhbGciOiJIUzI1NiIsInR5'
            }
        },
        'other-property': 'Other value here'
    };

    const filter_options = [
            [ 'personal.social', 
                [
                    'statistics.followers',
                    'statistics.following'
                ]
            ],
            [ 'personal.is_business'],
            [ 'personal.business_description'],
            [ 'meta.last-login'],
            [ 'meta.login-count'],
            [ 'meta', 
                [
                    ['info', [
                        'timestamp',
                        'user_id'
                    ]]
                ]
            ]
		];

        const filteredData = objFilter(sourceObject, filter_options); 
		const result = {
			'personal.social': {
                'statistics.followers': 1000,
                'statistics.following': 62000
            },
            'personal.is_business': true,
            'personal.business_description': 'Business description here',
            'meta.last-login': 1574187006717,
            'meta.login-count': 100,
            'meta': {
                'info': {
                    'timestamp': 1574178847,
                    'user_id': 'JhbGciOiJIUzI1NiIsInR5'
                }
            }
		};

Advanced Usage ( Nested Object, key paths and names)


    const filter_options = [
            [ 'personal.social', 
                [
                    'statistics.followers',
                    'statistics.following'
                ]
            ],
            [ 'personal.is_business'],
            [ 'personal.business_description'],
            [ 'meta.last-login'],
            [ 'meta.login-count'],
            [ 'meta', 
                [
                    ['info', [
                        'timestamp',
                        'user_id'
                    ]]
                ]
            ]
		];
        const filteredData = objFilter(sourceObject, filter_options); 

		const result = {
			'personal.social': {
                'statistics.followers': 1000,
                'statistics.following': 62000
            },
            'personal.is_business': true,
            'personal.business_description': 'Business description here',
            'meta.last-login': 1574187006717,
            'meta.login-count': 100,
            'meta': {
                'info': {
                    'timestamp': 1574178847,
                    'user_id': 'JhbGciOiJIUzI1NiIsInR5'
                }
            }

		};

Advanced Usage Samples

    const objFilter = require("nested-object-filter")
    const payload = {
        "personal": {
            "is_business": true,
            "business_name": "Business Name",
            "business_description": "Business description here",
            "gender": "Male",
        },
        "description": "more information as description"
        "meta": {
            "last-login": 1574187006717,
            "login-count": 100
        }
    }

If we wanted to extract only personal and description properties from the object. We will have to represent each property in an array like

    // Note that options container has to be an array.
    const filter_options = [
        // Filter options goes here.
    ]

    const filter_options = [
        ["personal"],
        ["description"] //This is how to specify each field to extract from object.
    ]

If a property is an object and you want to filter it's children you would have to add another array. Example if we wanted to extract business_name, business_description from the personal property we will structure object as

    const filter_options = [
        [
            "personal", [ //To filter fields from personal property
                "business_name", "business_description",
                //If a field here was an object which you would like to filter its properties, You will have to open another [bracket] like

                //note that here **another-field** is part of fields to filter for **personal** 
                [ "another-field", ["fields to filter"]]
            ] //Everything within this [bracket] is for the **personal** property
    ]

Summary

    const objFilter = require("nested-object-filter")
    const payload = {
        "personal": {
            "is_business": true,
            "business_name": "Business Name",
            "business_description": "Business description here",
            "gender": "Male",
        },
        "description": "more information as description"
        "meta": {
            "last-login": 1574187006717,
            "login-count": 100
        }
    }

    const filter_options = [
        ["personal", ["gender"]],
        ["description"] //This is how to specify each field to extract from object.
    ]

    const result = objFilter(payload, filter_options)

    console.lg(result)
    //Outputs
    {
        personal: {
            gender: "Male"
        },
        description: "more information as description"
    }

Full Usage

   const objFilter = require("nested-object-filter")
   const payload = {
           "personal": {
               "is_business": true,
               "business_name": "Business Name",
               "business_description": "Business description here",
               "gender": "Male",
               "city": "Tema",
               "Region": "Greater Accra",
               "country": "Ghana",
               "social": {
                   "whatsapp": "whatsapp-link-here",
                   "website": "website-link",
                   "facebook": "facebook-link",
                   "twitter": "twitter-link",
                   "instagram": "instagram-link",
                   "statistics": {
                       "followers": 1000,
                       "following": 62000,
                       "likes": 14000,
                       "dislikes": 9000
                   }
               }
           },
           "meta": {
               "last-login": 1574187006717,
               "login-count": 100,
               "info": {
                   "timestamp": 1574178847,
                   "user_id": "JhbGciOiJIUzI1NiIsInR5",
                   "req_id": "JzKv2itJ89XZ2NP0-T8oqZL2ppLM"
               }
           },
           "other-property": "Other value here"
       }

   const filter_options = [
           [
               "personal", [ "is_business", "business_name", ["social", ["website", "twitter", ["statistics", ["followers", "following"]] ]] ]
           ],
           [
               "meta", [ "last-login", "login-count", ["info", ["timestamp", "user_id"]] ]
           ],
           [ "other-property"]
       ]

   const filteredData = objFilter(payload, filter_options);
   // Output
       {
           "personal": {
               "is_business": true,
               "business_name": "Business Name",
               "social": {
                   "website": "website-link",
                   "twitter": "twitter-link",
                   "statistics": {
                       "followers": 1000,
                       "following": 62000
                   }
               }

           },
           "meta": {
               "last-login": 1574187006717,
               "login-count": 100,
               "info": {
                   "timestamp": 1574178847,
                   "user_id": "JhbGciOiJIUzI1NiIsInR5"
               }
           },
           "other-property": "Other value here"
       }