Vue.js filter id on array

How can I filter like this on vue.js?

$followid = [1,2,3,4,4,5];
foreach ($array as $element)
   if (in_array($element->id, $followid))
   endif
endforeach

3 thoughts on “Vue.js filter id on array”

  1. I ended up on this question trying to solve a similar problem, I think.

    I used a method as my solution, I had an array of users and I needed to match the current logged in user with the managers array of users on my model:

    methods: {
        isManager( task, current_user ) {
            return task.managers.find(x => x.id === current_user.id);
        }
    }
    

    Usage:

    <button v-if="isManager(task, current_user)">Approve</div>
    
    Reply
  2. You need to return bool in filter:

    ifInArray: function (value) {
        return this.editlistAssesments.indexOf(value) > -1 ? true : false;
    }
    
    Reply
  3. You should not use a filter for this usecase. I am not sure that you have access to ‘this’ in a filter. I encourage you to use a computed property that returns the array to display.

    computed: {
        actifOnline: function() {
            let self = this;
            return this.online.filter((o) => {
                return self.editlistAssesments.indexOf(o) > -1
            }
        }
    }
    
    Reply

Leave a Comment