[Vuejs]-Trying to filter an array based on inputs

0๐Ÿ‘

โœ…

I suggest using a Computed Property to store the list of tags

This will be calculated by Vue, every time "documents" changes.

computed: {

    uniqueTags(){
        const uniqueTagsSoFar = [];
        this.documents.forEach(document => {
            if (document && document.tags && document.tags.length>0){
                document.tags.forEach(tag=>{
                    if (!uniqueTagsSoFar.includes(tag)){
                        tags.push(tag)
                    }
                })
            }
        });
        return uniqueTagsSoFar
    }

}

Then, in your program, treat this.uniqueTags like a variable. It will be a list of tags, not including "", and listing each tag only once.

0๐Ÿ‘

One way for getting tags in computed property:

const documents = [
 {
    name: 'Some name',
    type: 'PDF',
    tags: [
        'tag 1',
        'tag 2',
        'tag 3'
    ],
    dateOfCreation: 'DD/MM'
  },
  {
    name: 'Some name',
    type: 'Excel',
    tags: [
        'tag 4',
        'tag 1',
        'tag 3'    ],
    dateOfCreation: 'DD/MM'
  },
  {
    name: 'Some name',
    type: 'Word',
    tags: '',
    dateOfCreation: 'DD/MM'
  }
]
const tags = documents.reduce((r,s) => {
  if (s.tags.length) r.push(s.tags)
  return r;
}, []).flat()
console.log([...new Set(tags)])

Leave a comment