Bhrungarajni
Bhrungarajni

Reputation: 2535

How to filter set of data based on current UserID in angular2

Hai i have a set of data, in that i must filter the data with my userId and display only that in the list. Please help. TS:

getDicomList() {
    this.service.getDicomList(params)
      .subscribe((res) => {
        this.dicomLists = res.Body.Data.dicomList;
        console.log(this.dicomLists, "shghds")
        let ids = this.dicomLists.map(item => item.CreatedBy);
       let filteredData = this.dicomLists.filter(item => ids.indexOf(item.CreatedBy) === -1);
      })

      i need to compare with my this.userId and item.CreatedBy

Console:

(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {StudyInstanceUID: "999.999.1.19941016.83000", StudyID: "ACC99", SeriesNumber: "2", AcquisitionNumber: "0", InstanceNumber: "101", …}
1: {StudyInstanceUID: "1.3.6.1.4.1.18047.1.11.10021841473447061672", StudyID: "", SeriesNumber: "3", AcquisitionNumber: "1", InstanceNumber: "49", …}
2: {StudyInstanceUID: "1.3.6.1.4.1.18047.1.11.10021841473447061672", StudyID: "", SeriesNumber: "3", AcquisitionNumber: "1", InstanceNumber: "49", …}
3: {StudyInstanceUID: "1.2.840.113619.2.55.3.604637917.369.1487635615.91", StudyID: "35112", SeriesNumber: "7", AcquisitionNumber: "1", InstanceNumber: "3", …}
4: {StudyInstanceUID: "1.3.6.1.4.1.18047.1.11.10021841473447061672", StudyID: "", SeriesNumber: "3", AcquisitionNumber: "1", InstanceNumber: "49", …}
5: {StudyInstanceUID: "1.2.840.113619.2.55.3.604637917.369.1487635615.91", StudyID: "35112", SeriesNumber: "7", AcquisitionNumber: "1", InstanceNumber: "5", …}
6: {StudyInstanceUID: "1.2.840.113619.2.55.3.604637917.369.1487635615.91", StudyID: "35112", SeriesNumber: "7", AcquisitionNumber: "1", InstanceNumber: "1", …}
7: {StudyInstanceUID: "1.2.840.113619.2.55.3.604637917.369.1487635615.91", StudyID: "35112", SeriesNumber: "7", AcquisitionNumber: "1", InstanceNumber: "4", …}
8: {StudyInstanceUID: "1.2.840.113619.2.55.3.604637917.369.1487635615.91", StudyID: "35112", SeriesNumber: "7", AcquisitionNumber: "1", InstanceNumber: "4", …}
9: {StudyInstanceUID: "1.3.6.1.4.1.18047.1.11.10021841473447061672", StudyID: "", SeriesNumber: "4", AcquisitionNumber: "1", InstanceNumber: "49", …}
length: 10
__proto__: Array(0)

Upvotes: 1

Views: 186

Answers (5)

H Mirindra
H Mirindra

Reputation: 94

this.service.getDicomList(params)
  .subscribe((res) => {
    this.dicomLists = res.Body.Data.dicomList;
    console.log(this.dicomLists, "shghds")
    let ids = this.dicomLists.map(item => item.CreatedBy); // the map is used to filter the properties of the object if necessary
   let filteredData = this.dicomLists.filter(item => ids.indexOf(item.CreatedBy) === -1); // Filter YES but the criteria in the filter is not good
  })

JUST LIKE THAT

this.service.getDicomList(params).subscribe((res) => {
    this.dicomLists = res.Body.Data.dicomList;
    this.dicomLists.filter(item => item.CreatedBy === this.userId);
})

Upvotes: 0

VizardCrawler
VizardCrawler

Reputation: 1303

You should always place a null check

        getDicomList() {
        this.service.getDicomList(params)
          .subscribe((res) => {
                 this.dicomLists = [];
                 // Null check else it will throw errors
                 if(res.body && res.body.Data 
                      && res.body.Data.dicomList
                      && res.body.Data.dicomList.length){
                   this.dicomLists = res.Body.Data.dicomList;
                 }
           return this.dicomLists.filter(item => this.userId == item.CreatedBy);
          })

Upvotes: 0

Karan Patel
Karan Patel

Reputation: 2289

You just need small modification.

getDicomList() {
    this.service.getDicomList(params)
      .subscribe((res) => {
        this.dicomLists = res.Body.Data.dicomList;

       let filteredData = this.dicomLists.filter(item => item.CreatedBy === this.userId);
      })

Upvotes: 2

Vivek Kumar
Vivek Kumar

Reputation: 5040

Can you try below code

let filteredData = this.dicomLists.filter(item => (this.userId == item.CreatedBy));

Upvotes: 1

Hitesh Kansagara
Hitesh Kansagara

Reputation: 3526

Do like this,

getDicomList() {
    this.service.getDicomList(params)
      .subscribe((res) => {
        this.dicomLists = res.Body.Data.dicomList;
        console.log(this.dicomLists, "shghds")
        //let ids = this.dicomLists.map(item => item.CreatedBy);
       let filteredData = this.dicomLists.filter(item => this.userId == item.CreatedBy);
      })

Upvotes: 2

Related Questions