pols-utils - v5.8.0
    Preparing search index...

    Function filter

    • Gets all elements in the array that match the given logical selector.

      Type Parameters

      • T

        The type of elements in the array.

      • K = T

        The return type of the transformation function, if provided.

      Parameters

      • array: T[]

        The target array to search in.

      • logicalSelector: PLogicalSelector<T>

        The selector used to identify the matching element. This can be either a predicate function or an object for property-based matching.

      • Optionaltransform: (element: T, i: number) => K

        A function to transform the selected element before returning it.

      Returns K[]

      A new array with all matching elements, optionally transformed.

      const pets = [
      { type: 'dog', name: 'Fido' },
      { type: 'dog', name: 'Chacalin' },
      { type: 'cat', name: 'Mr. Brown' },
      { type: 'cat', name: 'Night' },
      ]
      console.log(PUtilsArray.query(pets, pet => pet.type == 'dog'))
      // [
      // { type: 'dog', name: 'Fido' },
      // { type: 'dog', name: 'Chacalin' },
      // ]
      console.log(PUtilsArray.query(pets, { type: 'dog' }))
      // [
      // { type: 'dog', name: 'Fido' },
      // { type: 'dog', name: 'Chacalin' },
      // ]
      console.log(PUtilsArray.query(pets, { type: 'dog' }, pet => pet.name.toUpperCase())
      // [ 'FIDO', 'CHACALIN' ]