How to search a collection of JSON a certain register with Angle 2x?

0

I have a JSON in this format:

[
    {
        "agencia": "2342342",
        "digito_agencia": "2342",
        "numero_conta": "234234",
        "digito_conta": "23423",
        "_id": "5bff1a63a617f53414766c27",
        "createdAt": "2018-11-28T22:44:51.567Z",
        "updatedAt": "2018-11-28T22:44:51.574Z",
        "__v": 0,
        "bancos": {
            "nome_banco": "001 - BANCO DO BRASIL S/A",
            "_id": "5bf2eb4bc3910712f4604847",
            "id": "5bf2eb4bc3910712f4604847",
            "updatedAt": "2018-11-28T22:42:11.276Z",
            "contasJogador": "5bff0c013efde3235465a39f",
            "nome_do_banco": "5bff0dc1d40d01202418df4b",
            "contasjogador": [
                "5bff18eed8d38e1e7c99e83a"
            ]
        },
        "id": "5bff1a63a617f53414766c27",
        "jogador": {
            "nome": "Ramos Janones",
            "email": "[email protected]",
            "cpf": "03472925698",
            "celular": "34996320391",
            "telefone": "",
            "cep": "38300070",
            "endereco": "DEZESSEIS",
            "cidade": "Ituiutaba",
            "estado": "Minas Gerais",
            "bairro": "",
            "complemento": "",
            "usuario": "ramos",
            "senha": "teste123",
            "idppoker": "",
            "nickppocker": "ramosinfo",
            "numero": "",
            "_id": "5bf18c512af2e61ab879d2f7",
            "createdAt": "2018-11-18T15:59:13.735Z",
            "updatedAt": "2018-11-26T15:41:00.806Z",
            "__v": 0,
            "id": "5bf18c512af2e61ab879d2f7",
            "foto": null,
            "contasJogadors": null
        },
        "tipocontas": {
            "tipo_conta": "Conta Poupança",
            "_id": "5bff19328ff184285c7131df",
            "createdAt": "2018-11-28T22:39:46.676Z",
            "updatedAt": "2018-11-28T22:42:11.275Z",
            "__v": 0,
            "id": "5bff19328ff184285c7131df",
            "contasjogador": [
                "5bff18eed8d38e1e7c99e83a"
            ]
        }
    }
]

I need to get this data when the id of "jogador" is equal to the past, in this case the above JSON would be "id": "5bff19328ff184285c7131df" .

My service I have:

query(): Observable<any> {
  const id = localStorage.getItem('ref');
  return this.http.get(this.baseUrl, ${id});
}

Where localStorage.getItem('ref') is the player id stored in localStorage .

How do I get only the id of "jogador" as a filter in this array to play in *ngFor of the View?

    
asked by anonymous 28.11.2018 / 23:59

1 answer

3

Hello, how are you?

You can use filter , it returns the Array item that satisfies a condition, you can see more here

The way I did it

const jogadores = [
  {
    id: '1',
    jogador: {
      nome: 'Eduardo',
      email: '[email protected]'
    }
  },
  {
    id: '2',
    jogador: {
      nome: 'Jose',
      email: '[email protected]'
    }
  },
  {
    id: '3',
    jogador: {
      nome: 'Ribeiro',
      email: '[email protected]'
    }
  }
]

const filtraJogadores = (id, jogadores) => {
  return jogadores.filter(jogador => jogador.id === id)
}

let jogadorFiltrado = filtraJogadores('1', jogadores)

console.log(jogadorFiltrado)

Of course, my example is much simpler than yours, but the logic is that, but since you're using the Observable pattern, you'll have to use subscribe , something like that;

this.query()
  .subscribe(data => {
    this.jogador = this.filtraJogadores(ID_A_SER_FILTRADO, data)
  }, error => {
    console.error(error)
  })

If you have any questions, leave your comment below, if necessary I edit and make a more complete answer.

    
29.11.2018 / 00:35