根据数组的ID筛选数组

时间:2020-08-03 12:58:16

标签: javascript vue.js filter vue-router

我需要遍历我的filteredArray。指定数组中的对象具有多个属性,例如描述,时间,ID等。 我想根据ID过滤数组,所以我想取回具有所需ID的对象,因此我将能够使用它并打印出其描述,时间等。

这是我的求职信:

 <h1
          class="content"
          v-for="(record, index) of filteredRecords"
          :key="index"
          :class="{ 'is-active': index === activeSpan }"
        >
          
          <strong>{{ record.description }}</strong>
        </h1>

这是我过滤数组的方式:

filteredRecords() {
  return this.records.filter(record => {
    return record.id === this.id;
  });
}

如何从此函数返回整个对象?

我现在有什么:

records:[{
  description: "xy",
  time: 12,
  id: 5
}, {
  description: "xy",
  time: 12,
  id: 6

}, {
  description: "xy",
  time:12321,
  id:7
}]

例如,我想得到的是ID为7的所有内容:

所需的输出:

filteredRecords:[{
  description: "xy",
  time: 12321,
  id: 7
}]

2 个答案:

答案 0 :(得分:1)

我认为答案很简单,就是您缺少“ v-for”中的()。所以,不要这样:

v-for="(record, index) of filteredRecords"

您应该有这个:

v-for="(record, index) of filteredRecords()"

答案 1 :(得分:0)

问题不在过滤中,而是在vue部分中:

代替

v-for="(record, index) of filteredRecords"

使用

v-for="(record, index) in filteredRecords"