How to randomly render products after get API to obtain data in Vue? [closed]

Tags: ,



this.$http.get(api).then(response => {     
        vm.products = response.data.products; 
      });

If i have data about products and i want to randomly render products what should i do?

Answer

You can set a computed property where you shuffle your products list, something like this:

computed {
  shufflesProducts() {
    return vm.products.sort(() => Math.random() - 0.5);
  }
}


Source: stackoverflow