如何使用循环计算每个用户的百分比?

发布于 2025-01-11 07:11:40 字数 2423 浏览 0 评论 0原文

我这里有两个数组。其中一个项目和其他项目为主导。每个项目都有 idLead 就像外键一样。我已经创建了一些函数并且它们运行良好。我现在只想循环引导以了解项目中每个引导的部分。我的意思是有多少个项目的领先优势占百分比?

  ngOnInit(): void {
    // This is intentional

    this.SumOfLead();
    this.SumOfProjet();
    this.ProjetParAnnee('2020');
    this.leadParAnnee();
    this.LeadProjetSum('2');
  }
  leadList: any = [
    { id: '1', libelle: 'Vector', anneeCreation: '2020' },
    { id: '2', libelle: 'Salim', anneeCreation: '2021' },
    { id: '3', libelle: 'Antoine', anneeCreation: '2020' },
    { id: '4', libelle: 'Eya', anneeCreation: '2022' },
    { id: '5', libelle: 'Juliette', anneeCreation: '2021' },
    { id: '6', libelle: 'Anna', anneeCreation: '2021' },
  ];

  projetList: any = [
    {
      id: '1',
      libelle: 'EcoleVector',
      anneeCreation: '2021',
      idLead: '1',
    },
    { id: '2', libelle: 'Aramex', anneeCreation: '2021', idLead: '2' },
    { id: '3', libelle: 'SpeedFood', anneeCreation: '2021', idLead: '4' },
    { id: '4', libelle: 'Jumia', anneeCreation: '2022', idLead: '1' },
    { id: '5', libelle: 'Amazon', anneeCreation: '2020', idLead: '5' },
    { id: '6', libelle: 'AliBaba', anneeCreation: '2022', idLead: '6' },
    { id: '7', libelle: 'TikTok', anneeCreation: '2021', idLead: '3' },
    { id: '8', libelle: 'Teskerti', anneeCreation: '2022', idLead: '2' },
  ];
  SumOfLead() {
    let nombreLead = this.leadList.length;
    console.log(nombreLead, 'nombre de leads');
  }

  SumOfProjet() {
    let nombreProjet = this.projetList.length;
    console.log(nombreProjet, 'nombre de projets');
  }

  ProjetParAnnee(annee: string) {
    let filteredProjet = this.projetList.filter(
      (list: any) => list.anneeCreation === annee
    );
    console.log(filteredProjet, 'projets  crées en 2020');
  }

  leadParAnnee() {
    let annee = '2021';
    let filteredLead = this.leadList.filter(
      (lead: any) => lead.anneeCreation === annee
    );
    console.log(filteredLead, ' nombre de  lead crées  en  2021');
  }

  LeadProjetSum(id: any) {
    let nombreProjet = this.projetList.length;
    let SumProjetForOneLead = this.projetList.filter((sum: any) => sum.idLead === id);
    let part = (SumProjetForOneLead.length * 100) / nombreProjet;
    

    console.log(
      SumProjetForOneLead.length,
      'nombre de projet  par lead ',
      'part=',
      part + '%'
    );
  }

I have two arrays here. One of projects and other for lead. Every project have idLead like foreign key. I already created some functions and they are working fine. I want now just to loop on lead to know the part of every lead in projects. I mean how many projects has every lead in %?

  ngOnInit(): void {
    // This is intentional

    this.SumOfLead();
    this.SumOfProjet();
    this.ProjetParAnnee('2020');
    this.leadParAnnee();
    this.LeadProjetSum('2');
  }
  leadList: any = [
    { id: '1', libelle: 'Vector', anneeCreation: '2020' },
    { id: '2', libelle: 'Salim', anneeCreation: '2021' },
    { id: '3', libelle: 'Antoine', anneeCreation: '2020' },
    { id: '4', libelle: 'Eya', anneeCreation: '2022' },
    { id: '5', libelle: 'Juliette', anneeCreation: '2021' },
    { id: '6', libelle: 'Anna', anneeCreation: '2021' },
  ];

  projetList: any = [
    {
      id: '1',
      libelle: 'EcoleVector',
      anneeCreation: '2021',
      idLead: '1',
    },
    { id: '2', libelle: 'Aramex', anneeCreation: '2021', idLead: '2' },
    { id: '3', libelle: 'SpeedFood', anneeCreation: '2021', idLead: '4' },
    { id: '4', libelle: 'Jumia', anneeCreation: '2022', idLead: '1' },
    { id: '5', libelle: 'Amazon', anneeCreation: '2020', idLead: '5' },
    { id: '6', libelle: 'AliBaba', anneeCreation: '2022', idLead: '6' },
    { id: '7', libelle: 'TikTok', anneeCreation: '2021', idLead: '3' },
    { id: '8', libelle: 'Teskerti', anneeCreation: '2022', idLead: '2' },
  ];
  SumOfLead() {
    let nombreLead = this.leadList.length;
    console.log(nombreLead, 'nombre de leads');
  }

  SumOfProjet() {
    let nombreProjet = this.projetList.length;
    console.log(nombreProjet, 'nombre de projets');
  }

  ProjetParAnnee(annee: string) {
    let filteredProjet = this.projetList.filter(
      (list: any) => list.anneeCreation === annee
    );
    console.log(filteredProjet, 'projets  crées en 2020');
  }

  leadParAnnee() {
    let annee = '2021';
    let filteredLead = this.leadList.filter(
      (lead: any) => lead.anneeCreation === annee
    );
    console.log(filteredLead, ' nombre de  lead crées  en  2021');
  }

  LeadProjetSum(id: any) {
    let nombreProjet = this.projetList.length;
    let SumProjetForOneLead = this.projetList.filter((sum: any) => sum.idLead === id);
    let part = (SumProjetForOneLead.length * 100) / nombreProjet;
    

    console.log(
      SumProjetForOneLead.length,
      'nombre de projet  par lead ',
      'part=',
      part + '%'
    );
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满意归宿 2025-01-18 07:11:41

比创建一个函数并在 .html 中使用更好,向 LeadList 添加一个新属性:

const count=this.projetList.length;
this.leadList.forEach(x=>{
   x.part=this.projetList.filter(y=>y.idLead==x.id).length/count*100.0
})

或者您可以创建一个 countProjects 数组

const countProject=this.projetList.reduce((a,b)=>{
 const el=a.find(x=>x.id==b.idLead);
 if (!el)
   a.push({id:b.idLead,count:1})
  else
   el.count++

 return a
},[])

//and use
 this.leadList.forEach(x=>{
    const counter=countProject.find(y=>y.id==x.id)
    x.part=counter?counter.count/count*100.0:0
 })

Better than create a function and use in .html, add a new property to LeadList:

const count=this.projetList.length;
this.leadList.forEach(x=>{
   x.part=this.projetList.filter(y=>y.idLead==x.id).length/count*100.0
})

or you can create a countProjects array

const countProject=this.projetList.reduce((a,b)=>{
 const el=a.find(x=>x.id==b.idLead);
 if (!el)
   a.push({id:b.idLead,count:1})
  else
   el.count++

 return a
},[])

//and use
 this.leadList.forEach(x=>{
    const counter=countProject.find(y=>y.id==x.id)
    x.part=counter?counter.count/count*100.0:0
 })
酒中人 2025-01-18 07:11:41

谢谢我所做的每一个人,这只是一个简单的循环

leadProjetSum() {
   const nombreProjet = this.projetList.length;

   for (let lead of this.leadList) {
     let SumProjetByLead = this.projetList.filter(
       (project: any) => project.idLead === lead.id
     );

     const partlead = (SumProjetByLead.length * 100) / nombreProjet;

     console.log(
       lead.libelle,
       ':nombre de projets =',
       SumProjetByLead.length,
       ':part est =',
       partlead + '%'
     );
   }
 }

thanks every one i did it , it's just an easy loop

leadProjetSum() {
   const nombreProjet = this.projetList.length;

   for (let lead of this.leadList) {
     let SumProjetByLead = this.projetList.filter(
       (project: any) => project.idLead === lead.id
     );

     const partlead = (SumProjetByLead.length * 100) / nombreProjet;

     console.log(
       lead.libelle,
       ':nombre de projets =',
       SumProjetByLead.length,
       ':part est =',
       partlead + '%'
     );
   }
 }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文