【问题标题】:Check whether an array contains some data检查数组是否包含一些数据
【发布时间】:2018-04-29 23:53:30
【问题描述】:

我在一个组件中有 2 个变量,usersfriend

变量users 是一个数组。每次迭代包含idnameemail
变量friendpivot 具有相同的结构。

在我的组件文件(.vue 文件)中,我需要检查users 是否包含friend 而不使用循环,以便我可以在v-if 条件中使用它。

注意emailid 是唯一值。

如何查看?单行代码将非常有帮助。

例如:

users 有 2 次迭代

  • id:1
  • 姓名:名字
  • 电子邮件:first@example.com

  • id:2

  • 姓名:第二
  • 电子邮件:second@example.com

friend是这样的

  • id:2
  • 姓名:第二
  • 电子邮件:second@example.com
  • 枢轴:{...}

【问题讨论】:

标签: laravel vue.js components lodash


【解决方案1】:

使用_.find()lodash函数

_.find(users, {id: friend.id});

第一个参数是要检查的数组,第二个是要满足的条件。请记住,这将返回数组的特定迭代而不是 true 或 false,但您可以将其用作条件。如果它返回null则将其视为false,如果有则将其视为true。

【讨论】:

    【解决方案2】:

    尝试使用v-if

    <div v-if=":user.friend"></div>
    

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 2018-04-08
      • 2012-11-27
      • 1970-01-01
      • 2019-08-10
      • 2021-05-27
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      相关资源
      最近更新 更多