【发布时间】:2018-04-29 23:53:30
【问题描述】:
我在一个组件中有 2 个变量,users 和 friend
变量users 是一个数组。每次迭代包含id、name 和email
变量friend 与pivot 具有相同的结构。
在我的组件文件(.vue 文件)中,我需要检查users 是否包含friend 而不使用循环,以便我可以在v-if 条件中使用它。
注意:email 和 id 是唯一值。
如何查看?单行代码将非常有帮助。
例如:
users 有 2 次迭代
- id:1
- 姓名:名字
电子邮件:first@example.com
id:2
- 姓名:第二
- 电子邮件:second@example.com
friend是这样的
- id:2
- 姓名:第二
- 电子邮件:second@example.com
- 枢轴:{...}
【问题讨论】:
-
你能提供两个变量的例子吗?
-
您可以使用 sizeof() 来检查那里的天气是否为空
-
@GauravGupta 我想检查数组是否包含朋友变量的数据,有条件不检查数组是否为空
-
您可以使用数组
find()函数,类似于users.find(user => user.id === friend.id)。见developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… -
尝试使用lodash lodash.com/docs/4.17.4#findIndex
v-if="_.findIndex(friend, { 'id': 'userId' })"
标签: laravel vue.js components lodash