【发布时间】:2021-05-24 07:06:42
【问题描述】:
我有两个数组:users 和 projects。两者中的 ID 都是唯一的数字。一个项目可以有多个所有者,因此在projects 中,我有一组名为ownersId 的用户ID,它们链接到users 中用户的id,如下所示:
export const users = [{
id: 1,
givenName: 'Alexander',
surname: 'Kelly',
initials: 'AK'
}, {
id: 2,
givenName: 'Karen',
surname: 'Jones',
initials: 'KJ'
}, {
id: 3,
givenName: 'Casey',
surname: 'Fields',
initials: 'CF'
}, {
id: 4,
givenName: 'Sam',
surname: 'Johns',
initials: 'SJ'
}, {
id: 5,
givenName: 'Thomas',
surname: 'Smith',
initials: 'TS'
}, {
id: 6,
givenName: 'Jack',
surname: 'Jenson',
initials: 'JJ'
}];
export const projects = [{
id: 1,
name: 'Project 1',
ownersId: [
1,
2,
5,
6,
]}, {
id: 2,
name: 'Project 2',
ownersId: [
1,
3,
]}, {
id: 3,
name: 'Project 3',
ownersId: [
1,
2,
4,
3,
]}, {
id: 4,
name: 'Project 4',
ownersId: [
1,
]}, {
}]
我想要做的是循环查看project 的详细信息,我已经使用v-for 成功完成了该操作。我坚持的是在循环中显示一个循环,该循环使用 ownersId 字段中的 ID 显示所有 users 名称。
<template>
<div class="demo">
<div
v-for="project in projects"
v-bind:key="project.id"
>
<h4><strong>{{ project.name }}</strong></h4>
<div v-if="project.ownersId" >
Shared with {{ project.ownersId.length }} others
</div>
<div>
<!-- Loop of the list of names from IDs goes here -->
</div>
</div>
</div>
</template>
<script>
import { projects } from '../data/example';
import { users } from '../data/example';
export default {
name: "Demo",
data() {
return {
projects,
users,
}
}
}
</script>
【问题讨论】:
标签: javascript arrays vue.js vuejs2 vue-component