How to search a JSON registry with Angular 2x?

Asked

Viewed 164 times

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 the "jogador" is the same as the past, in this case the above JSON would be "id": "5bff19328ff184285c7131df".

My service 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 on localStorage.

How do I pick up just by the id of the "jogador" as filter in this array to play on *ngFor of View?

  • There are several questions on the website: https://answall.com/questions/81713 https://answall.com/questions/261168 https://answall.com/questions/330819 https://answall.com/questions/185806 https://answall.com/questions/292455

  • For more, just access this Google search

  • Did my question help you? If you don’t tell me more how I can help you :D

1 answer

1

Whoa, that’s all right?

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

Follow the shape I made

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)

Sure, my example is much simpler than yours, but that’s the logic, but how you’re using the pattern Observable, you will need to use the subscribe, something like;

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 will edit and make a more complete reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.