Angular + 2将值添加到对象数组并进行迭代

时间:2019-06-28 09:11:46

标签: angular data-structures

我有一个Proposal [id,名称,日期]和一个ProposalVote [id,用户,投票]对象。当我填写完一系列提案后,我想显示每条提案记录及其总票数。我打电话给我,获得了ProposalVote,但是现在我必须将它们一起显示在我的component.html中...

哪种数据结构是最好的选择?

Proposal: 1     TotalProposalVotes: 123
Proposal: 2     TotalProposalVote: 456

考虑下一步,我需要添加totalUSERProposalVotes(以及我接下来要添加的其他内容)

Proposal: 1     TotalProposalVotes: 123     totalUSERProposalVotes: 3
Proposal: 2     TotalProposalVote: 456      totalUSERProposalVotes: 4

我想我可以更改提案模型,但这看起来很麻烦。 在第一种情况下,HashMap听起来不错,但在第二种情况下却不然。

建议

import { Moment } from 'moment';
import { IVoteProposal } from 'app/shared/model/vote-proposal.model';

export const enum ProposalType {
  STUDY = 'STUDY',
  APPROVED = 'APPROVED',
  DEVELOPMENT = 'DEVELOPMENT',
  PRODUCTION = 'PRODUCTION'
}

export const enum ProposalRole {
  USER = 'USER',
  ORGANIZER = 'ORGANIZER',
  MANAGER = 'MANAGER',
  ADMIN = 'ADMIN'
}

export interface IProposal {
  id?: number;
  creationDate?: Moment;
  proposalName?: string;
  proposalType?: ProposalType;
  proposalRole?: ProposalRole;
  releaseDate?: Moment;
  isOpen?: boolean;
  isAccepted?: boolean;
  isPaid?: boolean;
  voteProposals?: IVoteProposal[];
  proposalUserId?: number;
  postId?: number;
}

export class Proposal implements IProposal {
  constructor(
    public id?: number,
    public creationDate?: Moment,
    public proposalName?: string,
    public proposalType?: ProposalType,
    public proposalRole?: ProposalRole,
    public releaseDate?: Moment,
    public isOpen?: boolean,
    public isAccepted?: boolean,
    public isPaid?: boolean,
    public voteProposals?: IVoteProposal[],
    public proposalUserId?: number,
    public postId?: number
  ) {
    this.isOpen = this.isOpen || false;
    this.isAccepted = this.isAccepted || false;
    this.isPaid = this.isPaid || false;
  }
}

然后投票:

import { Moment } from 'moment';

export interface IVoteProposal {
  id?: number;
  creationDate?: Moment;
  votePoints?: number;
  proposalId?: number;
  proposalUserId?: number;
}

export class VoteProposal implements IVoteProposal {
  constructor(
    public id?: number,
    public creationDate?: Moment,
    public votePoints?: number,
    public proposalId?: number,
    public proposalUserId?: number
  ) {}
}

感谢您的帮助。请,请尽可能多地解释您的解决方案。

0 个答案:

没有答案