0

I'm trying to populate an empty array with a declared array variable in a computed function. I tried this but with no luck:

data: {
  hashtags: []
},

computed: {
  filteredHashtags () {
    var defaultHashtags = [ '#hr', '#acc', '#sales' ];

    var fHashtags = 
     _.chain( messages )
    .pluck( 'hashtags' )
    .flatten()
    .map( 
      function ( tag ) { 
        return tag && tag.trim() ? '#' + tag : null; })
    .filter( Boolean )
    .value();  

    fHashtags = _.union( fHashtags, defaultHashtags );

    return data.hashtags = fHashtags;
  }
}

also, is there a better method to approach this?

2
  • Did you mean to data.hashtags = fHashtags instead? Or am I missing something? Commented Aug 30, 2017 at 20:34
  • I'm kind of new to Vue. Basically I'm assigning the value of fHastags to the hashtags in data. But yeah you're right. I'll make the edit Commented Aug 30, 2017 at 20:38

1 Answer 1

1

A computed property isn't really a good use case for this, because the computed value has to be referenced in order for it to be called. Instead, just make it a method and call it the method when your Vue is created.

data: {
  hashtags: []
},
methods: {
  filterHashtags() {
   // commented out stuff

    // set the data property with the filtered values
    this.hashtags = fHashtags;
  }
},
created(){
  this.filterHashtags();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.