【问题标题】:How to get the correct object in an nested array in Vue.js?如何在 Vue.js 的嵌套数组中获取正确的对象?
【发布时间】:2020-09-09 21:03:03
【问题描述】:

我使用Axios显示一个JSON数据,我已经成功了。但我想根据日期和时间显示一个对象,它现在显示所有数据,我需要对其进行过滤。

所以我想查看今天的日期并根据该日期显示对象,因此我想显示下一个即将发生的事件。 (24/05/2020)

我目前拥有的:

json:

{
    "doc": [
        {
            "data": {
                "events": {
                    "18807612": {
                        "_dt": {
                            "_doc": "time",
                            "time": "18:45",
                            "date": "14/05/20",
                            "tz": "UTC",
                            "tzoffset": 0,
                            "uts": 1566067500
                        },
                        "week": 33,
                        "teams": {
                            "home": {
                                "name": "name 1",
                                "mediumname": "name1",
                                "uid": 3014
                            },
                            "away": {
                                "name": "name 2",
                                "mediumname": "name 2",
                                "uid": 3020
                            }
                        }
                    },
                    "18807618": {
                        "_dt": {
                            "_doc": "time",
                            "time": "18:45",
                            "date": "24/05/20",
                            "tz": "UTC",
                            "tzoffset": 0,
                            "uts": 1566067500
                        },
                        "week": 33,
                        "teams": {
                            "home": {
                                "name": "name 1",
                                "mediumname": "name1",
                                "uid": 3014
                            },
                            "away": {
                                "name": "name 2",
                                "mediumname": "name2",
                                "uid": 3020
                            }
                        }
                    }
                }
            }
        }
    ]
}

商店:

async loadPosts({ commit }) {
    // Define urls pages
    const urlEvents = 'http://api.example.com/302020';

    // Set pages
    const [
      responseEvents
    ] = await Promise.all([
      // Responses pages
      this.$axios.get(urlEvents)
    ]);

    // variables pages
    this.events = responseEvents.data.doc[0].data.events

    // mutations pages
    commit('SET_EVENTS', this.events)
  }
},

mutations: {
  SET_EVENTS (state, events) {
    state.events = events;
  }
}

为了显示我使用的数据:

import {mapState} from 'vuex';

export default {
  name: 'NextMatch',
  mounted() {
    this.$store.dispatch('loadPosts')
  },
  computed: {
    ...mapState([
      'events'
    ])
  }
}

<h1>{{events}}</h1>

但这显示了所有数据,我试图获得的是带有"uid": 3014 的对象的第一个即将发生的事件。

所以我想显示主客队的日期、时间和名称。

如何通过过滤数据得到正确的数据?

【问题讨论】:

  • 我看不到fixtures 的来源,但大概您应该执行fixtures.doc.data.events.find(x =&gt; x.teams.home.uid === 3014) 之类的操作,并且可能将其放在计算属性中,而不是直接放在模板中
  • 夹具必须是事件,我编辑了我的消息。
  • 你有一个如何在计算属性中使用它的例子吗?
  • 当然,我在我留下的答案中描述了它。类似的东西应该可以工作。

标签: vue.js axios nuxt.js


【解决方案1】:

类似或类似的东西应该可以工作:

在你的 Vue 组件的&lt;template&gt;:

`<h1>{{selectedEvent._dt.date}}</h1>`

在你的 Vue 组件的&lt;script&gt;:

    props: {
        eventID: {
          type: Number,
          default: 3014
        },
    },
    computed: {
      ...mapState([
        'events'
      ]),
      eventsArr(){
        if (!this.events) return {} //make sure Object.entries gets object.
        return Object.entries(this.events)
      },
      selectedEvent(){
        const selectedArr = this.eventsArr.find(([eID, e]) => e.teams.home.uid === this.eventID)
        return selectedArr[1]
      } 
    }

【讨论】:

  • 这不起作用兄弟。也许是因为在控制器中使用了 mapState?查看我的第一条消息。
  • 我收到this.events.find is not a function
  • 哦,我明白了——events 不是一个数组,它是一个对象。所以我们必须把它转换成一个数组(使用类似Object.entries()的东西)才能在上面使用.find。我会更新答案。
  • 好吧,现在这真的是一个不同的问题,但我会做一些类似的事情,而不是 .find 使用 .reduce 并且,随着 .reduce 迭代,不断更新reduce“累加器”的值如果找到更新的事件,则为当前元素的值。
  • 对不起,我现在没时间了,但我希望你在这方面的进展顺利:)。一般来说,我只会多读一点 JS 中的函数式编程。
猜你喜欢
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 2016-01-01
  • 2021-09-01
相关资源
最近更新 更多