Rethic
Rethic

Reputation: 1081

Passing object path as parameter in typescript

I have a method that replaces one object in an array with another one. It will search by ID and change all records that match by the designated ID.

I would like to change this to a generic, reusable method that can operate in the same way for any object type I pass in. (EG: car.id, car.door.id, car.trunkMonkey.id, etc.)

Is there a way for me to pass the "element.car.id" path as a variable into this method?

updateArrayObject(item: any, data: any[]) {
        // Parse all objects in the array, looking for matches by ID
        data.forEach(element => {
            if (element.car.id === item.id) { // <-- Can I pass "element.car.id" into the method somehow?
                // Item ID matches. Replace the item.
                element.car = item;
            }
        });
    }

Upvotes: 2

Views: 9703

Answers (2)

El houcine bougarfaoui
El houcine bougarfaoui

Reputation: 37403

here is your generic function , cible can be car or door ... :

updateArrayObject(item: any, data: any[], cible) {
        // Parse all objects in the array, looking for matches by ID
        data.forEach(element => {
            if (element[cible].id === item.id) { // <-- Can I pass "element.car.id" into the method somehow?
                // Item ID matches. Replace the item.
                element[cible]= item;
            }
        });
    }

Upvotes: 0

Jason Kleban
Jason Kleban

Reputation: 20818

Some programs do this via string ("element.car.id") and parse the path at runtime. Not type-safe, unfortunately.

This here is a little more complicated and has its limits, but it is type-safe:

function updateArrayObject<T, R>( // ElementType, ReplacementType
    data: T[], // The array
    getter: (element: T) => R, // Getter of the thing that might be replaced
    setter: (element: T, replacement: R) => void, // Setter of the replacement, when appropriate
    keyComparer: (candidate: R, replacement: R) => boolean, // The matching predicate
    replacement: R) { // The replacement

    data.forEach(element => {
        if (keyComparer(getter(element), replacement)) {
            setter(element, replacement);
        }
    });
}

var sample: Element[] = [ /* ... */ ];

// Your car example
updateArrayObject<Element, Car>(
    sample,
    e => e.car,
    (e, r) => { e.car = r },
    (left, right) => left.id === right.id,
    {
        id: 42,
        door: { id: 0, name: 'driver' },
        trunkMonkey: { id: 0, tmName: 'bozo' }
    })

// Your trunkMonkey example
updateArrayObject<Element, TrunkMonkey>(
    sample,
    e => e.car.trunkMonkey,
    (e, r) => { e.car.trunkMonkey = r },
    (left, right) => left.id === right.id,
    {
         id: 0, tmName: 'bozo'
    })

// The various custom types involved.
interface Door { id: number, name: string }
interface TrunkMonkey { id : number, tmName: string }
interface Car {
    id: number,
    door: Door,
    trunkMonkey: TrunkMonkey
}
interface Element {
    car: Car,
    otherElementData: any
}

You might also research "functional lenses".

Upvotes: 3

Related Questions