Function filter

  • Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

    Note: Unlike remove, this method returns a new array.

    Type Parameters

    • T

    Parameters

    • collection: T[]

      The collection to iterate over.

    • Optional predicate: Predicate<T>

      The function invoked per iteration.

      Optional

    Returns T[]

    Returns the new filtered array.

    Since

    5.0.0

    See

    reject

    Example

    var users = [
    { 'user': 'barney', 'age': 36, 'active': true },
    { 'user': 'fred', 'age': 40, 'active': false }
    ];

    filter(users, function(o) { return !o.active; });
    // => objects for ['fred']

    // The `matches` iteratee shorthand.
    filter(users, { 'age': 36, 'active': true });
    // => objects for ['barney']

    // The `matchesProperty` iteratee shorthand.
    filter(users, ['active', false]);
    // => objects for ['fred']

    // The `property` iteratee shorthand.
    filter(users, 'active');
    // => objects for ['barney']
  • Type Parameters

    • T

    Parameters

    • collection: T[]
    • Optional predicate: any
      Optional

    Returns T[]