javascript - Passing object path as parameter in typescript - Stack Overflow

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

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;
            }
        });
    }

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;
            }
        });
    }
Share Improve this question asked Nov 9, 2017 at 16:24 RethicRethic 1,0814 gold badges21 silver badges37 bronze badges 1
  • This might help you: stackoverflow./questions/8817394/… – Łukasz Madej Commented Nov 9, 2017 at 16:34
Add a ment  | 

2 Answers 2

Reset to default 3

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 plicated 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".

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;
            }
        });
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745143150a4613522.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信