Aragorn
Aragorn

Reputation: 5289

Parameter the property used to filter an array

I have two functions that are almost same except that they compare different properties on the object inside an array. Is there a way to make that property as parameter and then combine these two functions into one?

First function, that compares property credit on the transactions array.

  public filterByCredit(filter: string) {

    switch (filter) {
      case 'Non-blanks':
        this.filteredTxs = this.localTransactions.filter(tx => tx.credit);
        break;
      case 'Blanks':
        this.filteredTxs = this.localTransactions.filter(tx => !tx.credit);
        break;
      case '> 500':
        this.filteredTxs = this.localTransactions.filter(tx => tx.credit > 500);
        break;
    }

  }

Second function that compares property debit

public filterByDebit(filter: string) {
    switch (filter) {
      case 'Non-blanks':
        this.filteredTxs = this.localTransactions.filter(tx => tx.debit);
        break;
      case 'Blanks':
        this.filteredTxs = this.localTransactions.filter(tx => !tx.debit);
        break;
      case '> 500':
        this.filteredTxs = this.localTransactions.filter(tx => tx.debit > 500);
        break;
    }

Upvotes: 0

Views: 43

Answers (3)

brk
brk

Reputation: 50316

You can try like this. Add one more parameter to the function which accepts the type that is credit or debit. Then inside the function set a variable depend on credit or debit

public filterByType(filter: string,type:string) {
  let __tp
  if(type==='credit'){
    __tp='credit'
  }
  else{
    __tp='debit'
  }
    switch (filter) {
      case 'Non-blanks':
        this.filteredTxs = this.localTransactions.filter(tx => tx[__tp]);
        break;
      case 'Blanks':
        this.filteredTxs = this.localTransactions.filter(tx => !tx[__tp]);
        break;
      case '> 500':
        this.filteredTxs = this.localTransactions.filter(tx => tx[__tp] > 500);
        break;
    }
}

Upvotes: 1

Ashish Ranjan
Ashish Ranjan

Reputation: 12960

Just send another param:

public filterByCredetDebit(filter, creditOrDebit) {
    switch (filter) {
        case 'Non-blanks':
            this.filteredTxs = this.localTransactions.filter(tx => tx[creditOrDebit]);
            break;
        case 'Blanks':
            this.filteredTxs = this.localTransactions.filter(tx => !tx[creditOrDebit]);
            break;
        case '> 500':
            this.filteredTxs = this.localTransactions.filter(tx => tx[creditOrDebit] > 500);
            break;
    }
}

Upvotes: 1

Zooly
Zooly

Reputation: 4787

You can pass a key parameter and use it as an accessor to your object property.

public filterByDebit(filter: string, key: string) {
    switch (filter) {
        case 'Non-blanks':
            this.filteredTxs = this.localTransactions.filter(tx => tx[key]);
            break;
        case 'Blanks':
            this.filteredTxs = this.localTransactions.filter(tx => !tx[key]);
            break;
        case '> 500':
            this.filteredTxs = this.localTransactions.filter(tx => tx[key] > 500);
            break;
    }
}

Upvotes: 3

Related Questions