我正在尝试获取某个值;例如,如果我得到"2019"作为这个sort_id,我想获取包含"2019"的元素作为sort_id.所以我必须得到 2 个作品元素作为结果进一步的问题)过滤器函数是从数组中过滤数组还是只从数组中过滤一个元素?
export default {
works : [
{
works_id: 'works1',
sort_title: 'years',
sort: '2019',
title: 'etd1',
year: '2019',
},
{
works_id: 'works2',
sort_title: 'years',
sort: '2019',
title: 'etd2',
year: '2019',
},
{
works_id: 'works3',
sort_title: 'years',
sort: '2018',
title: 'etd1',
year: '2018',
}
]
}
<template>
<div class="">
<div v-for="(o, index) in getWorksImgs" :key="index">
<div v-for="(a, index) in o" :key="index">
<img :src="a.mother">
</div>
</div>
</div>
<script>
import StorageWorks from '@/storage/works'
export default {
name: 'Works',
data() {
return {
workImgs : StorageWorks.works,
sort_id : this.$route.params.sort_id
}
},
computed: {
getWorksImgs(){
let worksImgs = this.worksImgs
worksImgs= worksImgs.filter(w => w.sort === this.sort_id )
return worksImgs
}
}
}
</script>
下面的代码将按照您的要求返回 2 个作品。
let filteredArrayOfObjects = this.works.filter(function (item) {
return item.sort === '2019';
});
console.log("You will get two objects returned here" + filteredArrayOfObjects)
在这里,它是根据条件过滤对象数组,而不是数组数组。