0

How to find unique values in Observable for specific property?

Here is my angular 2 component:

import { Component, OnInit, EventEmitter, Output } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { User } from '../../models/user';
import { UserService } from '../../services/user.service';

@Component({
    moduleId: module.id,
    selector: 'user-list',
    templateUrl: 'user-list.component.html'
})
export class UserListComponent implements OnInit {

    users: Observable<User[]>;
    uniqueGroup: any[];

    constructor(private userService: UserService) { }

    ngOnInit() {
        this.getUsers();
        this.getUniqueGroup();
    }

    getUsers(value?: string) {
        this.users = this.userService.getUsers(value);
    }

    getUniqueGroup() {
  this.uniqueGroup=this.users.forEach // I'm stuck here,
 //I can't get the unique Group values from users observable and assign them to the uniqueGroup array.
    }

}

Let say if the users observable have the following values ( I'm not sure how to draw the observable - I just put them in array to simplify what I want):

    users = [{
     Group: 'A',
     Name: 'SD'
    }, {
     Group: 'B',
     Name: 'FI'
    }, {
     Group: 'A',
     Name: 'MM'
    }, {
     Group: 'B',
     Name: 'CO'
    }];

I would like to get a new array of unique values for Group property.

I want to get ['A','B'].

Hope I put my question in a easy to understand way :)

user2662006
  • 2,246
  • 22
  • 16
  • This is like waaaaay broad. Please clarify your question and show us your effort :) – AT82 Feb 21 '17 at 20:49
  • 1
    And it seems your question has nothing to do with observables. The code in your question just shows an array. – JB Nizet Feb 21 '17 at 22:04
  • Possible duplicate of [Remove Duplicates from JavaScript Array](http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array) – JB Nizet Feb 21 '17 at 22:05
  • @JBNizet It is Observable array, I just want to simplify what I want in that array. Thanks – user2662006 Feb 21 '17 at 22:30
  • There is no such thing as an Observable array. Either its an Observable, or it's an array. It can't be both. What you have in your question is an array. It has absolutely nothing to do with Observables. – JB Nizet Feb 21 '17 at 22:33

0 Answers0