为什么我在使用forEach过滤数据后无法将数据推送到新对象中?

huangapple go评论56阅读模式
英文:

Why can I not push data to new object after filtering it with forEach?

问题

Sure, here's the translated code portion without the code itself:

我有一个包含另一个数组的数组,其中包含一个名称和一个日期。

我想创建一个第二个对象,其中包含数组中日期小于当前日期的项目。

因此,我执行了以下操作:

data() {
  return {
    currentDate: moment().locale(this.$i18n.locale).format('YYYY-MM-DD'),
    currentReservations: [],
    reservations: [
      {
        guests: [
          {
            name: 'John',
            reservationDate: '2023-06-01'
          },
          {
            name: 'Bob',
            reservationDate: '2023-06-30'
          }
        ]
      }
    ]
  };
},
methods: {
  sortDates(){
    const active = this.reservations;
    active.forEach(reservation => {
      if(reservation.guests.reservationDate < this.currentDate){
        this.currentReservations.push(reservation)
      }
    })
  }
}

如果运行此方法,即使有小于当前日期的日期,它也会返回一个空数组。

如果我使用v-for,我可以让它起作用,但我需要使用forEach,因为我需要在类似以下的Bootstrap表中返回过滤后的数据:

<b-table striped hover :items="currentReservations"></b-table>
英文:

I have a an array that contain another array, which contains a name and a data.
I would like to to create a second object that contains items from the array where the date is less than the current date.

So I did the following:

  data() {
    return {
      currentDate: moment().locale(this.$i18n.locale).format(&#39;YYYY-MM-DD&#39;),
      currentReservations: [],
      reservations: [
        {
          guests: [
            {
              name: &#39;John&#39;,
              reservationDate: &#39;2023-06-01&#39;
            },
            {
              name: &#39;Bob&#39;,
              reservationDate: &#39;2023-06-30&#39;
            }
          ]
        }
      ]
    };
  },
  methods: {
    sortDates(){
      const active = this.reservations;
      active.forEach(reservation =&gt; {
        if(reservation.guests.reservationDate &lt; this.currentDate){
          this.currentReservations.push(reservation)
        }
      })
    }
}

If I run this method, it just returns an empty array, even if there are dates that are less the current date.

I can get it to work if I use v-for, but I need to do a forEach, because I will need to return the filtered data in a bootstrap table like so:

&lt;b-table striped hover :items=&quot;currentReservations&quot;&gt;&lt;/b-table&gt;

答案1

得分: 3

你可以简单地使用一个计算属性:

computed: {
  currentReservations() {
    return this.reservations.filter((reservation) => reservation.guests.reservationDate < this.currentDate);
  },
},
英文:

You can simply use a computed:

  computed: {
    currentReservations() {
      return this.reservations.filter((reservation) =&gt; reservation.guests.reservationDate &lt; this.currentDate);
    },
  },

huangapple
  • 本文由 发表于 2023年6月13日 14:42:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定