[Vuejs]-Displaying prop in filtered function โ€“ axios, vue3

0๐Ÿ‘

โœ…


   

     export default {
              data() {
                return {
                  products: [],
                };
              },
              mounted() {
                   this.getSneakers();
              },
              methods: {
                 getSneakers() {
                 axios
                  .get('/src/stores/sneakers.json')
                   .then(response => { 
                  this.products = response.data ?? [];
                      })
                  }
               },
              computed: {
                    resultCount () {
                        return this.filteredProducts?.length ?? 0
                    },
                    brandName() {
                        return this.brand?.name ?? "";
                    },
                    filteredProducts () {
                        return this.products?.filter(
        product => product.name?.toLowerCase()?.includes(this.brandName.toLowerCase())
                   )
                    },
                },
              props: {
                brand: {
                  type: Object,
                },
              },
          }

0๐Ÿ‘

Choose the products that are named โ€˜Nikeโ€™ and pass them into filteredProducts. After that you can change the names using a computed property and use that in the template.

<template>
  <div 
    v-for="(item, i) in nikeProducts"
    :key="i"
  >
  </div>
</template>

<script>
export default {
  props: {
    brand: {
      type: Object
    }
  }
  data() {
    return {
      filteredProducts: [],
    };
  },
  mounted() {
    axios
      .get('/src/stores/sneakers.json')
      .then(response => { 
          const products = response.data
          this.filteredProducts = products.filter(product => product.name.include('Nike'));
      })
  },
  computed: {
    nikeProducts() {
      return this.filteredProducts.map(product => product.name = this.brand.name)
    }
  }
}
</script>

Leave a comment