简体   繁体   中英

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

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
        }
    }
}

You need to return bool in filter:

ifInArray: function (value) {
    return this.editlistAssesments.indexOf(value) > -1 ? true : false;
}

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM