Get item by another element from list in TypeScript

Hello,

Type Script in Ionic2 application:

How to get item from this.myList of check-boxes by another element. If I add elements to private myList: any = []; this way:

  this.myList.push({ id: i, name: myName, UID: obj.UID },);

where id: i is a loop iteration number, name: myName is a username, and UID: obj.UID is a non sequential specific number, then myList content should be:

    id| myName   | UID
    -------------------
    0 | sally    | 345
    1 | vikram   | 5487
    2 | kim      | 12
    3 | roy      | 2134

Goal is to find ID: obj.ID by myName value. For example get UID value: 12 by myName: kim

it is undefined and does not returns existed value, this way :

findByName() {
    const found = this.myList.find(({ name }) => this.searchName === name);

    if (found) {
        return found.UID;
    } else {
        return null;
    }
}

then I’ve tried differently, with adding: this.myList[myName] = { id: i, name: myName, UID: obj.UID }; to list: myList: {[name: string]: { id: number, name: string, UID: string }} = {}; but it same result undefined:

findByName(searchName: string) {
    const found = this.myList[searchName];

    if (found) {
        return found.UID;
    } else {
        return null;
    }
}

You’re trying to compare a name string to an object containing a name property. That’ll never hit anything.

$ node
> let a = [{id: 0, name: 'sally', uid: 345}, {id: 1, name: 'vikram', uid: 5487}, {id: 2, name: 'kim', uid: 12}, {id: 3, name: 'roy', uid: 2134}];
> let wantedname = 'kim';
> let found = a.find(e => e.name === wantedname);
> found.uid;
12

@rapropos

Hello,

Thank you for your answer

With list given as let a = ... as it is shown in example, your code works, but if I’m trying to use it with my real list, I got ERROR TypeError: Cannot read property 'uid' of undefined:

     let a = this.myList;
     let found = a.find(e => e.name === srcVal);

In console.log("Name: " + this.searchName, " UID: ", found.uid ); found.uid is undefined.

Seem like it is same structure