Search code examples
vue.jsvuejs2vue-componentvuex

How to set up lenght string in array


I don't understand what I'm doing wrong, can someone tell how to set up length string in array I need to trim product description

This is my code in Vue Component

<div v-for="product in randomProducts">
  <div>
    <img :src="'/uploads/img/products/' + product.image" alt="Card image cap">
    <div>
      <h4>{{product.price}} UAH</h4>
    </div>
    <div>
      <h4>{{product.name}}</h4>
      <p v-html="product.description">{{product.description.substring(0,3)+".."}}</p>
    </div>
    <div>
      <a href="#">More</a>
      <a href="#"><i class="fas fa-shopping-cart"></i></a>
    </div>
  </div>
</div>

This is my array

category_id: 1
created_at: "2020-05-14T16:02:12.000000Z"
description: "<p>New</p><p>New</p> <p>New</p><p>New</p><p>New</p><p>New</p><p>New</p><p>New</p><p>New</p><p>New</p><p>New</p><p>New</p>"
id: 4
image: "hzVjuKyTKw8D.jpeg"
keywords: "New"
name: "New"
price: "200"
slug: "new5ebd6b84337dc"
updated_at: "2020-05-14T16:02:12.000000Z"

Solution

  • First of all - it's not array, it's object. If you need just to trim your string, you can use usual js string function - trim():

    {{ product.description.trim() }}
    

    Otherwise, if you need to cut your string due to max length you can use substr():

    {{ product.description.substr(0, maxlength) }}