Giter Club home page Giter Club logo

js-gets's Introduction

jsGets

The function accepts 3 arguments: (object, path, clearFromUndefined)

If clearFromUndefined is true, will be returns only the values found at the path passed as argument. If is false will return the value where the path exists and undefined where the path does not exist.

Examples:

  • Given an object with this structure
{
    a : {
         b: 3,
         c: [
              { d: 4 }, 
              { d: 5 },
               { d: 2 }
          ]
     }
}

jsGets(myObject, 'a.c[].d') will return [4, 5, 2].

  • Given an array with this structure
[
    {
        bar: [
                {
                    lorem: 4
                }
            ]
    }, {
        bar: [
                {
                   other: {
                       ipsum: 5
                   }
                }, {
                    lorem: 6
                }
            ]
    }
]

jsGets(myObject, '[].bar[].lorem', false) will return [4, undefined, 6].

undefined because myObject[1].bar[0].lorem does not exist

  • Given an object with this structure
{
    foo: [
            {
                lol:  {
                    bar: [
                            {
                                lorem: [
                                    4,
                                    5
                                ]
                            }
                        ]
                }
            }, {
                bar: [
                        {
                            lorem: [
                                   6
                            ]
                        }, {
                            lorem: [
                                    7,
                                    8
                                ]
                        }
                ]
            }
    ]
}

jsGets(myObject, 'foo[].bar[].lorem[]', false) will return [undefined, 6, 7, 8].

jsGets(myObject, 'foo[].bar[].lorem[]', true) will return [6, 7, 8].

jsGets(myObject, 'foo[].bar[].lorem[]') will return [6, 7, 8], because clearFromUndefined is true as default.

js-gets's People

Contributors

dibericky avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.