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

    Type Alias PLogicalSelector<T>

    PLogicalSelector:
        | Partial<T> & Record<string, unknown>
        | ((element: T, index: number) => boolean)

    Represents a logical selector used to filter elements in an array. It supports two modes:

    Useful when workin with arrays of objects. Each property in the selector is compared against the corresponding property in every element of the array. If all values match, the element is included in the result.

    Nested properties can be accessed using dot notation.

    const myarray = [
    { prop1: 'oneA', prop2: 'twoA', prop3: 'threeA' },
    { prop1: 'oneB', prop2: 'twoB', prop3: 'threeB' },
    { prop1: 'oneC', prop2: 'twoC', prop3: { prop31: 'threeCA', prop32: 'threeCB' } },
    ]
    console.log(PUtilsArray.query(myarray, { prop2: 'twoB' })) // [{ prop1: 'oneB', prop2: 'twoB', prop3: 'threeB' }]
    console.log(PUtilsArray.query(myarray, { 'prop3.prop32': 'threeCB' })) // [{ prop1: 'oneC', prop2: 'twoC', prop3: { prop31: 'threeCA', prop32: 'threeCB' } }]

    Alternatively, a function can be provided. It will be called for each element in the array, and the element will be included in the result if the function returns true.

    const myarray = [
    { prop1: 'oneA', prop2: 'twoA', prop3: 'threeA' },
    { prop1: 'oneB', prop2: 'twoB', prop3: 'threeB' },
    { prop1: 'oneC', prop2: 'twoC', prop3: { prop31: 'threeCA', prop32: 'threeCB' } },
    ]
    console.log(PUtilsArray.query(myarray, element => element.prop2 == 'twoB')) // [{ prop1: 'oneB', prop2: 'twoB', prop3: 'threeB' }]
    console.log(PUtilsArray.query(myarray, element => element.prop3?.prop32 == 'threeCB' )) // [{ prop1: 'oneC', prop2: 'twoC', prop3: { prop31: 'threeCA', prop32: 'threeCB' } }]

    Type Parameters

    • T