Vuejs - How to get all unique values in a array (remove duplicates) using v-for

Mohammad Mahdi KouchakYazdi picture Mohammad Mahdi KouchakYazdi · Jan 7, 2019 · Viewed 10k times · Source

How to show only one button per every distinct date ?

can i use two v-for loops ? how to select distinct values in my loop?

<div v-for="question in allQuestions" >
  <button v-for="date in question.date">
    {{date}}
  </button>
</div>

Data model :

allQuestions : []
question : {'id' : '123' , 'date' : '25'}

Answer

Vucko picture Vucko · Jan 7, 2019

You can use Set:

The Set object lets you store unique values of any type, whether primitive values or object references.

MDN's example:

const numbers = [2, 3, 4, 4, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 5, 32, 3, 4, 5]

console.log([...new Set(numbers)])

Just modify it to your needs.