Skip to content
Advertisement

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

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?

Advertisement

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);
  }
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement