2

I have a JSON data from which I am displaying 'accountNumber' into a dropdown using *ngFor. Since there are multiple entries in JSON data with the same account number, I am seeing the same account number multiple times in the dropdown. enter image description here

html:

<div class="btn btn-default dropdown-toggle" type="button" 
  id="dropdownMenu" data-toggle="dropdown" aria-expanded="true">

  <span>Select</span>
  <span class="caret"></span>
  <ul class="select-menu" aria-labelledby="dropdownMenu">
    <li *ngFor="#account of accounts">{{account.accountNumber}}</li>
  </ul>
</div>

json:

`[
{
    "accountNumber": 7890,
    "transactionDate": "4/2/2016",
    "postingDate": "4/3/2016",
    "description": "Pok Pok Thai",
    "category": "Restaurants",
    "amount": 15.00
},
{
    "accountNumber": 7890,
    "transactionDate": "4/3/2016",
    "postingDate": "4/4/2016",
    "description": "Pok Pok Hai",
    "category": "Hotel",
    "amount": 25.00
},

{
    "accountNumber": 8901,
    "transactionDate": "4/6/2016",
    "postingDate": "4/7/2016",
    "description": "Pok Pok Fai",
    "category": "Dairy",
    "amount": 55.00
},
{
    "accountNumber": 8901,
    "transactionDate": "4/7/2016",
    "postingDate": "4/8/2016",
    "description": "Pok Pok Aai",
    "category": "Automotive",
    "amount": 65.00
},

{
    "accountNumber": 4567,
    "transactionDate": "4/9/2016",
    "postingDate": "4/10/2016",
    "description": "Pok Pok Cai",
    "category": "Healthcare",
    "amount": 85.00
},
{
    "accountNumber": 4567,
    "transactionDate": "4/10/2016",
    "postingDate": "4/11/2016",
    "description": "Pok Pok Dai",
    "category": "Healthcare",
    "amount": 95.00
},

{
    "accountNumber": 8901,
    "transactionDate": "4/12/2016",
    "postingDate": "4/13/2016",
    "description": "sit amet",
    "category": "Software",
    "amount": 115.00
}
 ]`

How can I avoid displaying duplicate values of the account number in the dropdown?I am assuming it will require a custom pipe but not sure how to do that.

I am new to Angular 2 and tried looking for the solution but couldn't find anything that suits my need.

KB_
  • 2,113
  • 2
  • 26
  • 28
abhi
  • 349
  • 2
  • 8
  • 24
  • Then you didn't search hard enough :) http://stackoverflow.com/questions/34417250/filtering-an-array-in-angular2 – Jan B. Jul 13 '16 at 22:30
  • You could load the account numbers into a set, and then iterate your ngFor over that. – bhzag Jul 13 '16 at 22:35

1 Answers1

1

There is already post explaing basic of pipes with examples: How to apply filters to *ngFor

See the working plunker for your case http://plnkr.co/edit/E7HlWeNJV2N3zwPfI51Q?p=preview .. I have used lodash library and its uniqBy function, then the pipe is really that simple:

declare var _: any; // lodash, not strictly typed

@Pipe({
    name: 'uniqFilter',
    pure: false
})
@Injectable()
    export class UniquePipe implements PipeTransform {
        transform(items: any[], args: any[]): any {

        // lodash uniqBy function
        return _.uniqBy(items, args);
    }
}

.. and the usage in your component:

<div>
    <ul>
        <li *ngFor="let account of accounts | uniqFilter: 'accountNumber'">{{ account.accountNumber }}</li>
    </ul>
</div>

EDIT: I've updated the plunker to latest Angular version, and added filtering parameter to the pipe.

Community
  • 1
  • 1
Petr Adam
  • 1,425
  • 11
  • 23
  • I am getting the error in this line return _.uniqBy(items, "accountNumber"); The error is [ts] Cannot find name '_'. any As I found here- http://stackoverflow.com/questions/34660265/importing-lodash-into-angular2-typescript-application, Do we need to import lodash before using it? I have referenced it like this in my index.html. Some problem with underscore(_) before uniqBy. – abhi Jul 14 '16 at 17:53
  • Yes, you need to reference lodash before using it, but thats in my plunker in index.html if you have opened it... try to add `declare var _: any;` before your @Pipe, I have updated the answer.. – Petr Adam Jul 14 '16 at 21:32
  • It worked. Thanks @Petr Adam. Only difference being, I had to use '#account of accounts' instead of 'let account of accounts' as it was throwing template parse error. – abhi Jul 14 '16 at 22:11
  • How would you use this to get uniques only of a specific type and by a changing field name...used something like this...*ngFor="let account of accounts | uniqFilter: 'accountNumber'" ? – Craig Mar 30 '17 at 09:04
  • I've updated the answer with pipe parameter, is that what you need? – Petr Adam Mar 30 '17 at 11:33
  • I've been trying to copy this example,.. doesn't seem to accept the accountNumber value – Yannick Mussche Oct 11 '22 at 19:03