dist/bbn.js
/**
     * Returns a new array with only the data matching the given filter.
     * 
     * The filtering arguments follow the same scheme as bbn.fn.search.
     * 
     * @method   filter
     * @global
     * @example
     * ```javascript
     * let ar = [
     *   {name: "Raiders of the lost ark", director: "Steven Spielberg", year: 1981, id: 589},
     *   {name: "Goonies", director: "Richard Donner", year: 1985, id: 689},
     *   {name: "Star wars", director: "George Lucas", year: 1977, id: 256},
     *   {name: "Jaws", director: "Steven Spielberg", year: 1975, id: 423}
     * ];
     * bbn.fn.filter(ar, {director: "Steven Spielberg"});
     * // [
     * //   {name: "Raiders of the lost ark", director: "Steven Spielberg", year: 1981, id: 589},
     * //   {name: "Jaws", director: "Steven Spielberg", year: 1975, id: 423}
     * // ]
     * bbn.fn.filter(ar, "director", "Steven Spielberg");
     * // Same result as the previous example
     * bbn.fn.filter(ar, {
     *   logic: "OR",
     *   conditions: [
     *     {
     *        field: "director",
     *        value: "Richard Donner"
     *     }, {
     *        field: "director",
     *        value: "George Lucas"
     *     }
     *   ]
     * );
     * // [
     * //   {name: "Goonies", director: "Richard Donner", year: 1985, id: 689},
     * //   {name: "Star wars", director: "George Lucas", year: 1977, id: 256},
     * // ]
     * ```
     *
     * @memberof bbn.fn
     * @param    {Array}                    arr       The subject array
     * @param    {(String|Object|Function)} prop      A property's name or a filter object or function
     * @param    {*}                        val       The value with which comparing the given property
     * @param    {String}                   operator  The operator to use for comparison with the value as used in bbn.fn.compare
     * @returns  {Array}                    A new filtered array
     */
Returns a new array with only the data matching the given filter.

The filtering arguments follow the same scheme as bbn.fn.search.




Returns a new array with only the data matching the given filter . The filtering arguments follow the same scheme as bbn . fn . search .
filter(arr, prop, val, operator) {
      if ( !bbn.fn.isArray(arr) ){
        throw new Error("Error in bbn.fn.filter: The first argument must be an array");
      }
      var filter = {},
          res = [],
          isFn = bbn.fn.isFunction(prop),
          isObj = bbn.fn.isObject(prop);
      if ( !prop || !arr.length ){
        return arr;
      }
      if ( arr.length ){
        if ( isObj || isFn ){
          operator = val;
          filter = prop;
        }
        else if ( typeof(prop) === 'string'){
          filter[prop] = val;
        }
        else{
          throw new Error("Search function error: The prop argument should be a string or an object");
        }
        if ( isFn ){
          bbn.fn.each(arr, (a, i) => {
            if ( filter(a, i) ){
              res.push(a);
            }
          })
        }
        else{
          filter = bbn.fn.filterToConditions(filter, operator);
          if ( filter.conditions && filter.logic ){
            bbn.fn.each(arr, (a) => {
              if ( bbn.fn.compareConditions(a, filter) ){
                res.push(a);
              }
            });
          }
        }
        return res;
      }
    }
filter ( arr , prop , val , operator ) { if ( ! bbn . fn . isArray ( arr ) ) { throw new Error ( "Error in bbn.fn.filter: The first argument must be an array" ) ; } var filter = { } , res = [ ] , isFn = bbn . fn . isFunction ( prop ) , isObj = bbn . fn . isObject ( prop ) ; if ( ! prop || ! arr . length ) { return arr ; } if ( arr . length ) { if ( isObj || isFn ) { operator = val ; filter = prop ; } else if ( typeof ( prop ) === 'string' ) { filter [ prop ] = val ; } else { throw new Error ( "Search function error: The prop argument should be a string or an object" ) ; } if ( isFn ) { bbn . fn . each ( arr , ( a , i ) => { if ( filter ( a , i ) ) { res . push ( a ) ; } } ) } else { filter = bbn . fn . filterToConditions ( filter , operator ) ; if ( filter . conditions && filter . logic ) { bbn . fn . each ( arr , ( a ) => { if ( bbn . fn . compareConditions ( a , filter ) ) { res . push ( a ) ; } } ) ; } } return res ; } }
Returns a new array with only the data matching the given filter.
Returns a new array with only the data matching the given filter .