A
A
asferot2019-01-16 14:20:07
Vue.js
asferot, 2019-01-16 14:20:07

How to pull an object from an array?

There is an array products, in which product.price, I need to pull it out from there in order to convert it to a monetary format. How can it be pulled out?
I did this, but it does not work, apparently not correctly)

computed:{
priceProduct(){
            var prc = this.products.reduce((product) => parseFloat(product.price), 0);
            return parseFloat(prc).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1 ").replace('.', ',');
        },
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-01-16
@asferot

Instead of a computed property, make a filter:

filters: {
  price: val => val.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1 ").replace('.', ','),
},

Accordingly, the template will be:
td {{ product.price | price }}
UPD. By the way, since you are displaying prices, instead of dancing with regular expressions, you can format the value using the toLocaleString function, it can work with money, it will be something like this:
price: val => val.toLocaleString('ru', {
  style: 'currency',
  currency: 'RUB',
}),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question