1

I want to remove or filter out the duplicate values in the output of my v-for loop. So, that 'John' appears only once instead of appearing twice in the output. Is that possible to do in vuejs ?

JS

var app = new Vue({
             el: "#app",
             data: function () {
                     return {
                       searchFields: ['number', 'name'],
                       search: '',
                       items: [
                               { name: 'Mary'},
                               { name: 'John'},
                               { name: 'John'},
                               { name: 'William'}
                             ]
                      };
             }
});

Codepen

3 Answers 3

3

So by checking the filters-documentation you can see that you can write your own custom filters.

So by having myFilter you can define what you want to have in a method.

v-for="item in items | filterBy myFilter">

Now adding the method it should something like this:

let arr = [];

var app = new Vue({
  el: "#app",
  data: function () {
    return {
      searchFields: ['number', 'name'],
      search: '',
      items: [
        { name: 'Mary'},
        { name: 'John'},
        { name: 'John'},
        { name: 'William'}
      ]
    };
  },
  // this is where your filter method is defined
  methods: {
    myFilter: function(val) {
      if(arr.indexOf(val.name) === -1) {
        arr.push(val.name);
        return val.name;
      }
    }
  }
});

What this does is check if your value is in your array arr. If not, it adds it to the array, preventing duplicate items appearing later on.

So only if your item.name hasn't been displayed already, your filter returns the name to the view.

Check it out

*Maybe there is a nicer/easier solution to this, but good enough for now :)

Sign up to request clarification or add additional context in comments.

3 Comments

hey @marczking...did u test ? your pen is not working !! it is just showing {{item.name}} in the output...check please
what do you mean? If I open the pen it displays 3 names instead of 4. It's working for me :S maybe there is an other error I'm not getting?
the search seams not be working. I'll have a look a little later
2

Vuejs filterBy provides 3 arguments to the filter function: value of the current item, the index of the item and the whole array. Following works without a temporary array:

methods: {
        myFilter: function (val, idx, arr) {
          for(var i = 0; i < idx; i++) {
            if(arr[i].name === val.name) {
              return false;
            }
          }
          return true;
        } 
      }

1 Comment

excellent solution man...this works even more perfect than the above one...because I do not need a temporary array in this case....simple awesome solution...thanks @himmet avsar...cheeers !!1
0

I used a computed value, as suggested by the Vuejs guide: https://v2.vuejs.org/v2/guide/list.html#Displaying-Filtered-Sorted-Results

computed: {
    uniqueNames: function() {
      var filtered_array = [];
      for(var i =0; i < this.items.length; i++) {
        if(filtered_array.indexOf(this.items[i].name) === -1) {
          filtered_array.push(this.items[i].name)
        }
      }
    return filtered_array;
    }
  }

With this v-for:

v-for="name in uniqueNames"

See my codepen

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.