简体   繁体   English

vue this.array.find 在方法/计算属性中给出未定义

[英]vue this.array.find in methods/computed properties gives undefined

I have an array (approvalOptions) that i receive from getters in a component, from which i need to find a certain value.我有一个数组 (approvalOptions),我从组件中的 getter 接收到它,我需要从中找到某个值。

Things i have tried:我尝试过的事情:

  1. Use it in methods (read from ...mapGetters) like so像这样在方法中使用它(从 ...mapGetters 读取)
methods: {
    approvalDisplayName(value) {
      console.log(this.approvalOptions)
      return this.approvalOptions.find(it => it.key === value).displayName;
    },
  },
  1. use it in computed (read from ...mapGetters) (code sample same as above, only difference being the use of computed properties' block instead of methods'在计算中使用它(从 ...mapGetters 读取)(代码示例与上面相同,唯一的区别是使用计算属性块而不是方法
  2. use it in methods (read from data properties) like so像这样在方法中使用它(从数据属性读取)
data() {
   return {
   approvalOptions: [...(objects with keys and values here)]
   }
 },
methods: {
   approvalDisplayName(value) {
     console.log(this.approvalOptions)
     return this.approvalOptions.find(it => it.key === value).displayName;
   },
 },

All three times I get an error in the console that approvalOptions.find(...) doesnt exist.我在控制台中都收到了三个错误,即批准选项.find(...) 不存在。 The console.log also prints an array in the console so i really don't understand what is going on here. console.log 还在控制台中打印了一个数组,所以我真的不明白这里发生了什么。

Maybe this can help you也许这可以帮助你

computed: {
    approvalOptions() {
      return [
        { key: 1, displayName: "Name 1" },
        { key: 2, displayName: "Name 2" }
      ];
    }
  },
  methods: {
    approvalDisplayName(options, value) {
      return options.find((it) => it.key === value).displayName;
    }
  },
  created() {
    console.log(this.approvalDisplayName(this.approvalOptions, 1));
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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