Filter an array and access the Response.body of a multidimensional array

Asked

Viewed 31 times

0

I’m trying to move forward on an angle project but I’ve been stranded for two weeks now.
How I can access a multidimensional array with the same attribute names?

Initially my json-server pulls 3 objects with nested arrays - and within them has the same attribute names as the others.

I’ve got 3 on the console initial request, that’s them:

{
0: Object { name: "lotes - Listar lotes", request: {…}, response: (2) […] }
​
1: Object { name: "lotes/{id} - Buscar lote", request: {…}, response: (1) […] }
​
2: Object { name: "lotes/{id}/registros - Listar registros lote", request: {…}, response: (1) […] }

the part I need is this:

{
            "name": "lotes - Listar lotes",
            "request": {
                "method": "GET",
                "header": [
                    {
                        "key": "Authorization",
                        "value": "Basic dGl2ZWE6UzRjTDdmUDl0RW1nYkNnTmxJRnBsekpBSlV5UGpVQjZkcUVKV2tnNDVYMGkw"
                    },
                    {
                        "key": "Content-Type",
                        "name": "Content-Type",
                        "value": "application/json",
                        "type": "text"
                    }
                ],
                "url": {
                    "raw": "https://{{HOST}}/api/assessorias/lotes",
                    "protocol": "https",
                    "host": [
                        "{{HOST}}"
                    ],
                    "path": [
                        "api",
                        "assessorias",
                        "lotes"
                    ],
                    "query": [
                        {
                            "key": "sort",
                            "value": "situacao",
                            "description": "Ordenação",
                            "disabled": true
                        },
                        {
                            "key": "page",
                            "value": "0",
                            "description": "Página (0, 1, 2...)",
                            "disabled": true
                        },
                        {
                            "key": "size",
                            "value": "50",
                            "description": "Tamanho da página",
                            "disabled": true
                        },
                        {
                            "key": "situacao",
                            "value": "ATIVO",
                            "description": "Situação - [ATIVO, INATIVO, REMOVIDO]",
                            "disabled": true
                        }
                    ]
                }
            },
            "response": [
                {
                    "name": "lotes - *",
                    "originalRequest": {
                        "method": "GET",
                        "header": [
                            {
                                "key": "Authorization",
                                "value": "Basic dGl2ZWE6UzRjTDdmUDl0RW1nYkNnTmxJRnBsekpBSlV5UGpVQjZkcUVKV2tnNDVYMGkw"
                            },
                            {
                                "key": "Content-Type",
                                "name": "Content-Type",
                                "value": "application/json",
                                "type": "text"
                            }
                        ],
                        "url": {
                            "raw": "https://{{HOST}}/api/assessorias/lotes",
                            "protocol": "https",
                            "host": [
                                "{{HOST}}"
                            ],
                            "path": [
                                "api",
                                "assessorias",
                                "lotes"
                            ],
                            "query": [
                                {
                                    "key": "sort",
                                    "value": "situacao",
                                    "description": "Ordenação",
                                    "disabled": true
                                },
                                {
                                    "key": "page",
                                    "value": "0",
                                    "description": "Página (0, 1, 2...)",
                                    "disabled": true
                                },
                                {
                                    "key": "size",
                                    "value": "50",
                                    "description": "Tamanho da página",
                                    "disabled": true
                                },
                                {
                                    "key": "situacao",
                                    "value": "ATIVO",
                                    "description": "Situação - [ATIVO, INATIVO, REMOVIDO]",
                                    "disabled": true
                                }
                            ]
                        }
                    },
                    "status": "OK",
                    "code": 200,
                    "_postman_previewlanguage": "json",
                    "header": [
                        {
                            "key": "Date",
                            "value": "Fri, 07 May 2021 19:01:18 GMT"
                        },
                        {
                            "key": "Content-Type",
                            "value": "application/json"
                        },
                        {
                            "key": "Content-Length",
                            "value": "219"
                        },
                        {
                            "key": "Connection",
                            "value": "keep-alive"
                        },
                        {
                            "key": "Content-Encoding",
                            "value": "gzip"
                        },
                        {
                            "key": "Expires",
                            "value": "0"
                        },
                        {
                            "key": "x-meta-has-next",
                            "value": "false"
                        },
                        {
                            "key": "Cache-Control",
                            "value": "no-cache, no-store, max-age=0, must-revalidate"
                        },
                        {
                            "key": "x-meta-total-count",
                            "value": "5"
                        },
                        {
                            "key": "x-meta-current-size",
                            "value": "10"
                        },
                        {
                            "key": "X-XSS-Protection",
                            "value": "1; mode=block"
                        },
                        {
                            "key": "Pragma",
                            "value": "no-cache"
                        },
                        {
                            "key": "X-RateLimit-Limit-hmlassessoria",
                            "value": "350"
                        },
                        {
                            "key": "X-RateLimit-Reset-hmlassessoria",
                            "value": "1000"
                        },
                        {
                            "key": "X-RateLimit-Remaining-Quota-hmlassessoria_assessoria.a",
                            "value": "999"
                        },
                        {
                            "key": "x-meta-current-page",
                            "value": "0"
                        },
                        {
                            "key": "X-Frame-Options",
                            "value": "DENY"
                        },
                        {
                            "key": "x-meta-has-previous",
                            "value": "false"
                        },
                        {
                            "key": "X-RateLimit-Limit-hmlassessoria_assessoria.a",
                            "value": "10"
                        },
                        {
                            "key": "X-RateLimit-Remaining-hmlassessoria",
                            "value": "349"
                        },
                        {
                            "key": "X-RateLimit-Reset-hmlassessoria_assessoria.a",
                            "value": "15000"
                        },
                        {
                            "key": "X-Content-Type-Options",
                            "value": "nosniff"
                        },
                        {
                            "key": "x-meta-total-pages",
                            "value": "1"
                        },
                        {
                            "key": "x-meta-number-elements",
                            "value": "5"
                        },
                        {
                            "key": "X-RateLimit-Remaining-hmlassessoria_assessoria.a",
                            "value": "9"
                        },
                        {
                            "key": "X-RateLimit-Quota-hmlassessoria_assessoria.a",
                            "value": "1000"
                        }
                    ],
                    "cookie": [],
                    "body": "[\n    {\n        \"id\": \"1081673531130966016\",\n        \"dataProcessamento\": \"2021-04-27\",\n        \"quantidadeClientes\": \"0\",\n        \"quantidadeParcelas\": \"0\",\n        \"situacao\": \"INATIVO\"\n    },\n    {\n        \"id\": \"1081682225134571520\",\n        \"dataProcessamento\": \"2021-04-28\",\n        \"quantidadeClientes\": \"1576\",\n        \"quantidadeParcelas\": \"35500\",\n        \"situacao\": \"INATIVO\"\n    },\n    {\n        \"id\": \"1083447097597952000\",\n        \"dataProcessamento\": \"2021-04-29\",\n        \"quantidadeClientes\": \"1577\",\n        \"quantidadeParcelas\": \"35513\",\n        \"situacao\": \"INATIVO\"\n    },\n    {\n        \"id\": \"1083450368450428928\",\n        \"dataProcessamento\": \"2021-04-30\",\n        \"quantidadeClientes\": \"1577\",\n        \"quantidadeParcelas\": \"35513\",\n        \"situacao\": \"INATIVO\"\n    },\n    {\n        \"id\": \"1083817674049376256\",\n        \"dataProcessamento\": \"2021-05-01\",\n        \"quantidadeClientes\": \"1577\",\n        \"quantidadeParcelas\": \"35513\",\n        \"situacao\": \"ATIVO\"\n    }\n]"
                },
                {
                    "name": "lotes - Ativos",
                    "originalRequest": {
                        "method": "GET",
                        "header": [
                            {
                                "key": "Authorization",
                                "value": "Basic dGl2ZWE6UzRjTDdmUDl0RW1nYkNnTmxJRnBsekpBSlV5UGpVQjZkcUVKV2tnNDVYMGkw"
                            },
                            {
                                "key": "Content-Type",
                                "name": "Content-Type",
                                "value": "application/json",
                                "type": "text"
                            }
                        ],
                        "url": {
                            "raw": "https://{{HOST}}/api/assessorias/lotes?situacao=ATIVO",
                            "protocol": "https",
                            "host": [
                                "{{HOST}}"
                            ],
                            "path": [
                                "api",
                                "assessorias",
                                "lotes"
                            ],
                            "query": [
                                {
                                    "key": "sort",
                                    "value": "situacao",
                                    "description": "Ordenação",
                                    "disabled": true
                                },
                                {
                                    "key": "page",
                                    "value": "0",
                                    "description": "Página (0, 1, 2...)",
                                    "disabled": true
                                },
                                {
                                    "key": "size",
                                    "value": "50",
                                    "description": "Tamanho da página",
                                    "disabled": true
                                },
                                {
                                    "key": "situacao",
                                    "value": "ATIVO",
                                    "description": "Situação - [ATIVO, INATIVO, REMOVIDO]"
                                }
                            ]
                        }
                    },
                    "status": "OK",
                    "code": 200,
                    "_postman_previewlanguage": "json",
                    "header": [
                        {
                            "key": "Date",
                            "value": "Wed, 28 Apr 2021 22:22:09 GMT"
                        },
                        {
                            "key": "Content-Type",
                            "value": "application/json"
                        },
                        {
                            "key": "Content-Length",
                            "value": "134"
                        },
                        {
                            "key": "Connection",
                            "value": "keep-alive"
                        },
                        {
                            "key": "Content-Encoding",
                            "value": "gzip"
                        },
                        {
                            "key": "Expires",
                            "value": "0"
                        },
                        {
                            "key": "x-meta-has-next",
                            "value": "false"
                        },
                        {
                            "key": "Cache-Control",
                            "value": "no-cache, no-store, max-age=0, must-revalidate"
                        },
                        {
                            "key": "x-meta-total-count",
                            "value": "1"
                        },
                        {
                            "key": "x-meta-current-size",
                            "value": "10"
                        },
                        {
                            "key": "X-XSS-Protection",
                            "value": "1; mode=block"
                        },
                        {
                            "key": "Pragma",
                            "value": "no-cache"
                        },
                        {
                            "key": "X-RateLimit-Limit-hmlassessoria",
                            "value": "350"
                        },
                        {
                            "key": "X-RateLimit-Reset-hmlassessoria",
                            "value": "1000"
                        },
                        {
                            "key": "X-RateLimit-Remaining-Quota-hmlassessoria_assessoria.a",
                            "value": "1000"
                        },
                        {
                            "key": "x-meta-current-page",
                            "value": "0"
                        },
                        {
                            "key": "X-Frame-Options",
                            "value": "DENY"
                        },
                        {
                            "key": "x-meta-has-previous",
                            "value": "false"
                        },
                        {
                            "key": "X-RateLimit-Limit-hmlassessoria_assessoria.a",
                            "value": "10"
                        },
                        {
                            "key": "X-RateLimit-Remaining-hmlassessoria",
                            "value": "349"
                        },
                        {
                            "key": "X-RateLimit-Reset-hmlassessoria_assessoria.a",
                            "value": "15000"
                        },
                        {
                            "key": "X-Content-Type-Options",
                            "value": "nosniff"
                        },
                        {
                            "key": "x-meta-total-pages",
                            "value": "1"
                        },
                        {
                            "key": "x-meta-number-elements",
                            "value": "1"
                        },
                        {
                            "key": "X-RateLimit-Remaining-hmlassessoria_assessoria.a",
                            "value": "9"
                        },
                        {
                            "key": "X-RateLimit-Quota-hmlassessoria_assessoria.a",
                            "value": "1000"
                        }
                    ],
                    "cookie": [],
                    "body": "[\n    {\n        \"id\": \"1081682225134571520\",\n        \"dataProcessamento\": \"2021-04-28\",\n        \"quantidadeClientes\": \"1576\",\n        \"quantidadeParcelas\": \"35500\",\n        \"situacao\": \"ATIVO\"\n    }\n]"
                }
            ]

a the request service is this:

  readonly apiURL = `http://localhost:3000`;
  readonly lotesURL = 'item';

        getLotes(situacao: situacao.ATIVO){
        let params = new HttpParams()
        .set('situacao', situacao);
        return this.httpClient.get<any[]>(`${this.apiURL}/${this.lotesURL}/`,{params: params})
    }

this is my component:

import { Lote, response, situacao} from 'src/app/services/lotes.service';
import { Component, OnInit, ChangeDetectorRef} from '@angular/core';
import { Injectable } from '@angular/core';
import { environment } from './../../../environments/environment';
import { HeaderService } from 'src/app/services/header-service.service';
import { LotesService } from 'src/app/services/lotes.service';
import { PageEvent } from '@angular/material/paginator';


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

//status 200 sempre
@Injectable({
  providedIn: 'root'
})

export class LotesComponent implements OnInit {
  //onde irá ser gerada a array dos lotes e
  //configuração das colunas da tabela que irá ser mostrada
  MostrarColunas: string[] = ['id', 'dataProcessamento', 'quantidadeClientes', "quantidadeParcelas", "situacao"];
  lotes: Lote[] = [];
  dataSource = this.lotes;
  response : response[] = [];
  pageslice = this.lotes.slice(0, 5);

  //paginação
  pageSize = 3;
  pageIndex = 0;
  pageSizeOptions = [3, 6, 9];
  showFirstLastButtons = true;
  handlePageEvent(event: PageEvent) {
    const startIndex = event.pageIndex * event.pageSize;
    let endIndex = startIndex + event.pageSize;
    if (endIndex > this.lotes.length) {
      endIndex = this.lotes.length;
    }
    this.pageslice = this.lotes.slice(startIndex, endIndex);
    this.pageSize = event.pageSize;
    this.pageIndex = event.pageIndex;
  }

  constructor(private loteService: LotesService,
    private headerService: HeaderService,
    private changeDetectorRef: ChangeDetectorRef) {
      this.lotes = [];
  }

  ngOnInit() {
    this.headerService.setTitle('Lotes');
    this.BuscaLotes();
    this.changeDetectorRef.detectChanges();
  }
  
  BuscaLotes() {
    this.loteService.getLotes(situacao.ATIVO).subscribe(
      (response:any) => {
        this.response = response;
        this.lotes = response;
        console.log(this.lotes);
      }
    );
  }
}

my question is: i need to find a way to access the first only Replay.body(Lots - List Lots), but in the request it pulls these 3.. I’ve tried to use rxjs Pluck, filter by index, query strings, map, filter, but none met me correctly, or probably not used in the right way

Obs: It’s a third-party API so I don’t have access to it to make modifications.. That’s their design standard

  • 1

    It was not clear your doubt, it would be better if you post the full array!

  • postei! json is a bit large and has passed the character limit, but it’s basically the part I need to access.. is a very complex json, in case I need to access the body of active lots

  • Cara is very confused this return, but, so posted will have to take the variable that saved the return and then access the property sponse guy this.data.response walk pq is an array with a map, foreach or for that is best. Make a if to see if the property name is equal to "lotes - Ativos" and if it is equal access property body that tbm is an array or will have to go through it tbm. This is by the json that posted!

  • 1

    is what I thought. I think it helps me enough.. I had an idea of how to do it but I didn’t know exactly... I’m pretty new to angular and typescript so I’m doing everything half scared.. I’ll try here and if I can update the topic with the resolution, thank you!

No answers

Browser other questions tagged

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