Filter number of items with certain property

0

Good afternoon guys,

I'm trying to filter the number of items in an array, which has a specific property and a certain value (in the case: 'status: Open') in my array, to insert into a summary type box in home with this format:

Belowarethefilesforthissectioninmyproject.

Template:

<divclass="row summary">

<div class="col-lg-4 col-md-12 col-sm-12 col-xs-12">
  <div class="summary-card total">
    <h1>{{bugList ? bugList.length : ''}}</h1>
    <div class="description">
      <div class="item" >
        Bugs total
      </div>
    </div>
    <img src="assets/img/folder.png" alt="bugs-total-icon">
  </div>
</div>

<div class="col-lg-4 col-md-12 col-sm-12 col-xs-12">
  <div class="summary-card abertos">
    <h1>XX</h1>
    <div class="description">
      <div class="item">
        Bugs abertos
      </div>
    </div>
    <img src="assets/img/stopwatch.png" alt="bugs-abertos-icon">
  </div>
</div>

<div class="col-lg-4 col-md-12 col-sm-12 col-xs-12">
  <div class="summary-card resolvidos">
    <h1>XX</h1>
    <div class="description">
      <div class="item">
        Bugs resolvidos
      </div>
    </div>
    <img src="assets/img/success.png" alt="bugs-resolvidos-icon">
  </div>
</div>

<div class="col-lg-4 col-md-12 col-sm-12 col-xs-12" hidden>
  <div class="summary-card enviadas">
    <h1>XX</h1>
    <div class="description">
      <div class="item">
        Sugestões enviadas
      </div>
    </div>
    <img src="assets/img/edit.png" alt="sugestoes-enviadas-icon">
  </div>
</div>

Component:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { BugList } from './../bugs-list/bug-list.model';
import { BugsService } from './../bugs-list/bugs-list.service';
import { Bug } from '../add-bug/add-bug.model';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})

export class HomeComponent {

bug: any

bugList: BugList[];


constructor(
private router: Router,
private bugsService: BugsService) { }

ngOnInit() {
this.bugsService.bugLists()
.subscribe(response => { this.bugList = response
  localStorage.setItem("bugs", JSON.stringify(response));
})}}

Interface:

export interface BugList {
id: string;
titulo: string;
usuario: string;
criacao: string;
atualizacao: string;
status: string;
}

BugService:

import {Injectable} from '@angular/core';
import {Http, Headers, HttpModule} from '@angular/http';
import { FormGroup } from '@angular/forms';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import {BugList} from './bug-list.model';
import { Bug } from '../add-bug/add-bug.model'


@Injectable()
export class BugsService {

bug: Bug;
formEditBug: FormGroup;

constructor(
    private http: Http,
) {}

    bugLists(): Observable<BugList[]> {
        const headers = new Headers();
        headers.append('Content-Type', 'application/json')
        headers.append('Access-Control-Allow-Origin', '*')
        const token = localStorage.getItem('token')
        headers.append('Authorization', 'Bearer ${token}')

    return this.http.get('${PORTAL_API}/Bug/listar-bugs', { headers })
        // .map( response => response )
        .map( response => response.json() )

    }}

I need to filter the amount of Open Bugs and Closed Bugs by the Array Status property, can anyone help me?

    
asked by anonymous 12.12.2018 / 17:44

1 answer

1

You can use the arrays / p>

ngOnInit() {
this.bugsService.bugLists()
.subscribe(response => { 
  this.bugList = response
  this.bugsAbertos=this.bugList.filter(bug=>bug.status==='aberto')
  localStorage.setItem("bugs", JSON.stringify(response));
})}}
    
12.12.2018 / 18:16