【发布时间】:2020-08-03 12:58:16
【问题描述】:
我需要遍历我的过滤数组。指定数组中的对象具有描述、时间、id 等多个属性。 我想根据 ID 过滤数组,所以我想取回具有所需 ID 的对象,这样我就可以使用它并打印出它的描述、时间等。
这是我的 v-for:
<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
}]
【问题讨论】:
-
“我需要过滤器中的整个数组”——
.filter()的重点是过滤 out 原始数组的一些元素。如果您不想这样做,那么使用.filter()是没有意义的。 -
如果您需要整个数组,则不是过滤,它们是相反的概念。请更好地解释“过滤器”应该做什么
-
我知道了,我应该怎么做,比如映射?
-
你想达到什么目的?请提供之前的数据示例和之后需要的数据
-
你所做的已经是你想要的。您当前的代码有什么问题?
标签: javascript vue.js filter vue-router