T
T
tempfolder2021-03-10 01:29:40
typescript
tempfolder, 2021-03-10 01:29:40

How to recognize a function argument as a property of another object?

Tell me how you can

this function
sortAny(contacts: ContactList[], type?: string, direction?: string) {
    
    const sortArr = contacts.sort(function (a, b) {
      const x = a.type;
      const y = b.type;
      if (x > y) {
        return 1;
      }
      if (x < y) {
        return -1;
      }
      return 0;
    });

    if (direction === 'down') {
      return sortArr;
    }
    if (direction === 'up') {
      return sortArr.reverse();
    }
    return contacts;
  }
specifically in this
code snippet
const x = a.type;
      const y = b.type;
, assign the value of the type argument as a property of the object a and b.
For example, if a type argument was passed with the value "fname", it was substituted for a.type and b.type.
Now throws an error, which is logical:
any
Property 'type' does not exist on type 'ContactList'.ts(2339)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-03-10
@tempfolder

const x = a[type];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question