Reputation: 216
I'm looking for some help to remove duplicate words, commas and [] in an array of objects output in a child component.
PARENT
<Countries
v-for="(result, index) in countries"
:result="result"
:index="index"
/>
CHILD
<div v-if="country" >
{{country}}
</div>
mounted () {
this.country = [...new Set(this.result.results.country)];
},
data: function () {
return {
name: this.result.results.country
}
JSON data
"result": [
"results": {
"country": "Poland, France, Germany, Poland, Latvia, Estonia, Germany, Germany, France",
},
"results": {
"country": "Poland, France, Germany, Poland, Latvia, Estonia, Germany, Germany, France",
},
"results": {
"country": "Poland, France, Germany, Germany, France",
},
"results": {
"country": "Greece, Spain",
},
.....
This is the OUTPUT I am getting [ "G", "r", "e", "c", ", ",", "S", "p", "a", "i", "n",", " ]
It splits the data and removes duplicate letters. How can I get it to just split the words, remove duplicate entries and remove the quotes and the commas? So the output would just be
Greece, Spain
Thank you
Upvotes: 0
Views: 50
Reputation: 27192
We are using Set
object is to remove the duplicates from an input which could be any iterable value. i.e. string
or an array
.
As per your requirement, First you have to convert that countries string into an array by using Array.split()
method and then you can remove the duplicate names.
Live Demo :
const country = "Poland, France, Germany, Poland, Latvia, Estonia, Germany, Germany, France";
const arr = country.split(', ');
console.log([...new Set(arr)].join());
Upvotes: 1
Reputation: 6501
You should split the CSV string
mounted () {
this.country = [...new Set(this.result.results.country.split(',')];
}
Upvotes: 1
Reputation: 1
Try to split the string by ', '
and create the set based on the array from the split result :
this.country = [...new Set(this.result.results.country.split(', '))];
Upvotes: 2