Home > Mobile >  Get array of a column value in array of objects in Angular 12
Get array of a column value in array of objects in Angular 12

Time:01-06

i have an Angular 12 project and i want to retrieve all results in artisticBehaviour (except those who are NULL or same e.g. Actor, Actor) column in Users table.

ts function that i call all users from Users table.

allArtistsSet() {
    this.userData.allArtists(this.offset).pipe(
      map((data: any) => {
        if (data.success) {
            this.allArtists = data.artistsFeed;

           this.creativenessArtists = this.allArtists.artisticBehaviour;
          //this.creativenessArtists = this.allArtists.filter(data.artisticBehaviour)
           //this.creativenessArtists = [...this.allArtists.artisticBehaviour];
           //return this.creativenessArtists.filter(allArtists => allArtists.artisticBehaviour === this.creativenessArtists);
        }
      })
    ).subscribe()
  }

hmtl.

<select>
            <option disabled>Creative Behaviour</option>
            <option>Creativeness</option>
            <option *ngFor="let creative of creativenessArtists" value="{{creative}}">{{creative}}</option>
            </select>

CodePudding user response:

You can use filter() function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

CodePudding user response:

I assume you wish to store the data received from API in one of the variable with a condition that uses filter if yes then do do it this way,

allArtistsSet() {
    this.userData.allArtists(this.offset).subscribe( 
data=>{
if (data.success) {
            this.allArtists = data.artistsFeed;

           this.creativenessArtists = this.allArtists.artisticBehaviour;
          //this.creativenessArtists = this.allArtists.filter(data.artisticBehaviour)
           //this.creativenessArtists = [...this.allArtists.artisticBehaviour];
          this.creativenessArtists = data.artistsFeed.filter(allArtists => allArtists.artisticBehaviour === this.creativenessArtists);
        }
});
  }
  •  Tags:  
  • Related