vuejs如何检查数组中是否存在key/ID

vuejs how to check if key/ID exist in the array

我有一个 roles 数组,其中列出了所有可用的角色。每个用户都分配有 1 个或多个角色,名为 user_roles.

我将所有 roles 列为复选框,默认标记为未选中。 当我 select 一个用户时,我希望能够检查列表中的角色 ID 是否与我的 user_roles 数组中的 ID 匹配。

在 php/html 中,我可以使用

来实现
<?=in_array($user_roles, $role->id) ? 'checked' : '' ?>

如何在 vuejs 中实现相同的功能?

我正在使用以下示例尝试相同的想法:

<input type="checkbox" name="role[]" :value="role.id" :id="'role' + role.id" :checked="in_array(user_roles, role.id)" />

使用数组的indexOf方法:

:checked="user_roles.indexOf(role.id) >= 0"