Nutella Pancake Machine

    @risan/filter-obj

    1.0.3 • Public • Published

    Filter Object

    Build Status Test Covarage Greenkeeper Latest Version

    Filter object by its property names or values or both.

    Installation

    $ npm install @risan/filter-obj

    CDN

    The library is available over a CDN:

    <script src="https://unpkg.com/@risan/filter-obj@latest/dist/filter-obj.umd.js"></script>
    
    <!-- Or the minified version -->
    <script src="https://unpkg.com/@risan/filter-obj@latest/dist/filter-obj.umd.min.js"></script>

    Usage

    Filter by property values:

    import filterObj from "@risan/filter-obj";
    
    const obj = {
      num1: 1,
      num2: 2,
      num3: 3,
      num4: 4,
    };
    
    const filtered = filterObj(obj, value => value % 2 == 0);
    
    // { num2: 2, num4: 4 }
    console.log(filtered);

    Filter by property keys:

    import filterObj from "@risan/filter-obj";
    
    const obj = {
      name: "john",
      languages: ["JavaScript"],
      age: 20
    };
    
    const filtered = filterObj(obj, (value, key) => key === "languages");
    
    // { languages: [ 'JavaScript' ] }
    console.log(filtered);

    You can pass an array as a predicate argument. It will keep the given property names.

    import filterObj from "@risan/filter-obj";
    
    const obj = {
      name: "john",
      languages: ["JavaScript"],
      age: 20
    };
    
    const filtered = filterObj(obj, ["name", "age"]);
    
    // { name: 'john', age: 20 }
    console.log(filtered);

    If no predicate argument is passed, it will use Boolean function against each property values.

    import filterObj from "@risan/filter-obj";
    
    const obj = {
      str: "foo",
      empty_str: "",
      awesome: true,
      not_awesome: false,
      number: 123,
      zero: 0,
      arr: [],
      otherObj: {}
    };
    
    const filtered = filterObj(obj);
    
    // { str: "foo", awesome: true, number: 123, arr: [], otherObj: {} }
    console.log(filtered);

    You can get the reference to the entire object from the third argument of the predicate function.

    import filterObj from "@risan/filter-obj";
    
    const myObj = {
      name: "john",
      languages: ["JavaScript"],
      status: false
    };
    
    const filtered = filterObj(myObj, (value, key, obj) => {
      return obj.name === "john" && Boolean(value);
    });
    
    // { name: 'john', languages: [ 'JavaScript' ] }
    console.log(filtered);

    API

    filterObj(obj, [predicate])

    Parameters

    • obj (Object): The object to filter.
    • predicate (optional Function|Array): The function to invoke on each iteration to filter the object. If Array is given, it will keep all property names listed on that array. If none is given, it will use Boolean function against each property values.

    The predicate function will receive three arguments with the following order:

    • value: The object property value.
    • key: The object property key.
    • obj: The object to filter.

    Returns

    It returns a filtered Object.

    License

    MIT © Risan Bagja Pradana

    Keywords

    Install

    npm i @risan/filter-obj

    DownloadsWeekly Downloads

    9

    Version

    1.0.3

    License

    MIT

    Unpacked Size

    36.6 kB

    Total Files

    6

    Last publish

    Collaborators

    • risan