【问题标题】:Not able to filter using lodash in computed Nuxt [duplicate]无法在计算的 Nuxt 中使用 lodash 进行过滤 [重复]
【发布时间】:2022-01-22 01:08:06
【问题描述】:

我无法在 nuxt 的计算属性中使用 lodash 进行过滤。
我从 API 获取博客列表,在 Vue 调试器中出现以下错误

(评估时出错)

我要过滤已删除状态为false的数据列表。

这里是 JS

<script>
import { _ } from 'lodash'

export default {
  data() {
    return {
      data: [
        {
          deleted: {
            status: false,
            date: '2021-12-20T10:18:33.231Z',
          },
          blogUID: '*********',
          title: 'Guide To Visiting Inflatable Island In The New Normal',
        },
        {
          deleted: {
            status: false,
            date: '2021-12-20T10:18:33.231Z',
          },
          blogUID: '*********',
          title: '24 Best Places to Celebrate New Year in India',
        },
        {
          deleted: {
            status: false,
            date: '2021-12-20T10:18:33.231Z',
          },
          blogUID: '*********',
          title: 'Top Things to Do in Dubai',
        },
        {
          deleted: {
            status: true,
            date: '2021-12-20T10:18:33.231Z',
          },
          blogUID: '*********',
          title: 'Best Places to Celebrate New Year 2022',
        },
      ],
    }
  },

  computed: {
    activeData() {
      return _.filter(this.data, { 'deleted.status': false })
    },
  },
}
</script>

【问题讨论】:

    标签: vue.js nuxt.js lodash


    【解决方案1】:

    你真的不需要 lodash。

    像这样使用vanilla JS filter 方法

    return this.data.filter((el) => !el.deleted.status)
    

    或者,如果您想检查与 false 的严格相等性,而不是仅使用虚假值(undefinednull 等...)

    return this.data.filter((el) => el.deleted.status === false)
    

    【讨论】:

      【解决方案2】:

      虽然没有必要使用 lodash,但回答您的问题

      return _.filter(fields, 'deleted.status', false)
      

      return _.filter(fields, {deleted: {status: false}})
      

      【讨论】:

        猜你喜欢
        • 2019-03-10
        • 1970-01-01
        • 2015-02-26
        • 2018-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-14
        相关资源
        最近更新 更多