Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS: How to output a comma separated array?

Tags:

vue.js

vuejs2

I know that in VueJS I can loop through an array:

<span v-for="(element, index) in list">{{ element }}</span> 

But what if I wanted a list that is comma separated? For example, if list = ["alice", "bob", "chuck"], then the above would output:

<span>alice</span><span>bob</span><span>chuck</span> 

What I want, though, is:

<span>alice</span>, <span>bob</span>, <span>chuck</span> 

Is this possible?

like image 622
Ben Avatar asked Feb 09 '17 06:02

Ben


People also ask

How do you store comma-separated values in an array?

Answer: Use the split() Method You can use the JavaScript split() method to split a string using a specific separator such as comma ( , ), space, etc. If separator is an empty string, the string is converted to an array of characters.

How do you convert an array to a comma with strings?

To convert an array to a comma-separated string, call the join() method on the array, passing it a string containing a comma as a parameter. The join method returns a string containing all array elements joined by the provided separator.

How do you convert a list of integers to a comma-separated string?

Use the join() Function to Convert a List to a Comma-Separated String in Python. The join() function combines the elements of an iterable and returns a string.

How do you read a comma-separated string in Java?

In order to parse a comma-delimited String, you can just provide a "," as a delimiter and it will return an array of String containing individual values. The split() function internally uses Java's regular expression API (java. util. regex) to do its job.


1 Answers

If all you care about is comma separation, use Javascript's built-in join method:

{{ list.join(', ') }} 
like image 97
René Roth Avatar answered Sep 25 '22 14:09

René Roth