数组方法与箭头函数使用返回undefined

做项目的时候:

有这么一个需求,当id相同的时候,将所对应的对象返回给计算属性

视频内的代码运行正常,且无报错:

  const currCategory = computed(()=>{
      return menuList.value.find(item => item.id === category.value)
    })

我的代码,打印计算属性发现value为undefined:

    const categoryShow = computed(() => {
      return menuList.value.find((item) => {item.id === currentId.value});
    });

 

一开始的时候没发现使用了中括号(习惯了),总是没办法拿到值,怀疑是不是menuList有问题,但是打印正确

 

 打印id对比也能正常打印

这时候我就怀疑上了find函数,对比发现我所写的代码多了一个中括号,去掉之后数据正常获取。

 

终究还是对箭头函数不够理解:

加入中括号后需要return

    const categoryShow = computed(() => {
      return menuList.value.find((item) => {return item.id === currentId.value});
    });

或者单行执行忽略中括号

 

见笑了。。。

posted @ 2021-11-23 10:09  Jacky02  阅读(137)  评论(0)    收藏  举报