[Vuejs]-Get first several only items of the array with fetch

0👍

You can use Array.prototype.slice to get only the first 5 items.

people: [],
getAllProducts(searchInput, num = 5) {
  fetch("https://jsonplaceholder.typicode.com/users")
    .then((response) => response.json())
    .then((res) => {
      // Set people to only a portion of the array, from index 0 to num
      this.people = res.slice(0, num);
    });
},

In your code, instead of adding a single item to the array in each iteration of the while loop, you were setting the people property to the entire response array.

Leave a comment