Function findTargetObject

  • This function receives a list of objects and a target object.
    It will try to find an object in the list that matches the properties of the target object.
    If a match is found, it returns the matching object and its index in the list, otherwise it returns undefined and -1.

    Since

    v0.0.1

    Returns

    If the target object is found, the function returns a tuple with the matching object and its index in the list. If the target object is not found, the function returns a tuple with undefined and -1.

    Example

    // find an object in the list
    const exampleListA = [{ ID: 1, label: 'label1' }, { ID: 2, label: 'label2' }]
    console.log(
    findTargetObject(exampleListA, { ID: 1 })
    )
    => [{ ID: 1, label: 'label1' }, 0]

    Example

    // not found
    const exampleListB = [{ ID: 1, label: 'label1' }, { ID: 2, label: 'label2' }]
    console.log(
    findTargetObject(exampleListB, { ID: 3 })
    )
    => [undefined, -1]

    Type Parameters

    • T extends Record<string, any>

    Parameters

    • objectArray: T[]

      An array of objects, where each object represents a record with key-value pairs.

    • target: Record<string, any>

      An object representing the record to be searched in the list.

    Returns [T, number] | [undefined, -1]