如何在vue计算属性内映射结果

时间:2019-01-28 07:07:31

标签: vue.js computed-properties

我们的应用程序有多个位置,每个位置的访问组均为a,b,c或d。 我们的用户有一个accessGroups ['a','c']等数组。

在我们的位置页面上,myLocations应该返回location.accessGroup包含在其accessGroup数组中的位置。

computed: {
  ...mapState(['currentUser', 'locations', 'userProfile']),
}
myLocations: function() {
    return this.locations.filter((location) => {
      return location.accessGroup === this.userProfile.accessGroup
    })
  },

1 个答案:

答案 0 :(得分:0)

好像您正在将字符串与数组进行比较。试试这个:

// assuming userProfile.accessGroup is an array of strings like ['a', 'c'] etc
// and location.accessGroup is a string: 'a', 'b', 'c', or 'd'
myLocations () {
  return this.locations.filter((location) => {
    return this.userProfile.accessGroup.indexOf(location.accessGroup) >= 0
  })
}
相关问题