Return certain array values

Asked

Viewed 145 times

2

Consume an Api whose return is an array with 10 objects inside, I want to show on the screen only the first five of the array, which are always the latest in the Api, as you can see in the code below, in the test with the while I got 5, but only the last 5, I tried to give a reverse() before the length, However, he added to the list:

let api = [
  {
    "id": 44,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Filtro de data cartão virtual ",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 39,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Testes mais testes",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 27,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferi 100,00 reais",
    "institutionName": "0221 20** **** 4895",
  },
  {
    "id": 26,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transf. de 350 reais",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 25,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferência de Duzentão",
    "institutionName": "0221 30** **** 0114",
  },
  {
    "id": 11,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "fdfdfd",
    "institutionName": "0221 20** **** 0843",
  },
  {
    "id": 9,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferindo 400",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 8,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Teste 200",
    "institutionName": "0321 20** **** 1995",
  },
  {
    "id": 7,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "recarga 100",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 5,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Primeira Recarga",
    "institutionName": "0221 20** **** 0843",
  }
]

let i = 5;

while(i < api.length) {
  console.log(api[i]);
  i++;
}

How can I take only the first five results of the array, and save them if possible in an array, because I am using in a table with Angular and do a ngFor.

  • 1

    try changing Let i = 5; for Let i = 0; and while(i < api.length) for while(i < 5)

  • 1

    @Sumback, post a reply, it is good to get registered on the site.

2 answers

3


Use the method splice, passing the start and end position parameter to assign the first 5 positions in a new array; Ai in his ngFor use the array created for iteration.

let api = [
  {
    "id": 44,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Filtro de data cartão virtual ",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 39,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Testes mais testes",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 27,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferi 100,00 reais",
    "institutionName": "0221 20** **** 4895",
  },
  {
    "id": 26,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transf. de 350 reais",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 25,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferência de Duzentão",
    "institutionName": "0221 30** **** 0114",
  },
  {
    "id": 11,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "fdfdfd",
    "institutionName": "0221 20** **** 0843",
  },
  {
    "id": 9,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferindo 400",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 8,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Teste 200",
    "institutionName": "0321 20** **** 1995",
  },
  {
    "id": 7,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "recarga 100",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 5,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Primeira Recarga",
    "institutionName": "0221 20** **** 0843",
  }
]

let arr = api.splice(0,5);
console.log(arr)

2

You can use the .slice(0, 5) which gives you exactly the first 5. In other words, create a new array with only a part of the initial array.

The API of .slice is .slice(<indice onde começar>, <indice onde acabar>).

let api = [{
    "id": 44,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Filtro de data cartão virtual ",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 39,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Testes mais testes",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 27,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferi 100,00 reais",
    "institutionName": "0221 20** **** 4895",
  },
  {
    "id": 26,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transf. de 350 reais",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 25,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferência de Duzentão",
    "institutionName": "0221 30** **** 0114",
  },
  {
    "id": 11,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "fdfdfd",
    "institutionName": "0221 20** **** 0843",
  },
  {
    "id": 9,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferindo 400",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 8,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Teste 200",
    "institutionName": "0321 20** **** 1995",
  },
  {
    "id": 7,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "recarga 100",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 5,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Primeira Recarga",
    "institutionName": "0221 20** **** 0843",
  }
]

const primeiros5 = api.slice(0, 5);

console.log(primeiros5);

Browser other questions tagged

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