J
J
jazzus2019-07-03 21:43:04
Vue.js
jazzus, 2019-07-03 21:43:04

How to remove comma at the end of v-for?

It seems like a simple task, but what did I think.
The code

<span v-for="name in names" :key="name">{{name}}, </span>

The loop ends with a comma and needs to be removed somehow.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-07-03
@jazzus

Add a comma depending on the index of the current element:

<span v-for="(n, i) in names">{{ n }}{{ i < n.length - 1 ? ', ' : '' }}</span>

Or to hell with v-for, glue the array into a string and output it into one span:
<span v-text="names.join(', ')"><span>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question