2 回答
TA贡献2016条经验 获得超9个赞
可能有一个computed道具适合您(检查 ID/课程名称交集)
const mainVue = new Vue({
el: '#mainContent',
data: {
myCourses: [{Course: "A"}, {Course: "B"}],
allCourses: [{Course: "A"}, {Course: "B"}, {Course: "C"}, {Course: "A"}],
},
computed: {
allCoursesCounts() {
return this.allCourses.reduce((acc, { Course }) => {
acc[Course] = (acc[Course] || 0) + 1
return acc
}, {})
},
subscribedTo() {
const allCoursesCounts = this.allCoursesCounts
return this.myCourses.map(({ Course }) => ({
courseName: Course,
amountOfUsers: allCoursesCounts[Course] || 0,
}))
}
}
});
<div id="mainContent">
<div v-for="({ courseName, amountOfUsers }) of subscribedTo" :key='courseName'>
<span>Course {{ courseName }} - {{ amountOfUsers }}</span>
</div>
</div>
TA贡献2019条经验 获得超9个赞
您想要过滤 myCourses 中出现的所有课程
allCourses.filter(course => {
return myCourses.find(m => {
return m.Course === course.Course
})
}).length
添加回答
举报