具有两个indefinides值的Typescript比较

时间:2018-03-06 15:30:44

标签: angular typescript

我正在寻找解决这种情况的最佳方法:

我有2个下拉列表:

let combination1: string;
let combination2: string;

我还有一个对象CurriculumVitae 和一个数组这个对象

obj: Curriculum;
obj.$name;
obj.$age;
obj.$gender;
obj.$country;
obj.$count; //--this value that is increment if two combinations are true...
...and more others properties...

curriculumArray: Array<Curriculum>  

这个数组在其他地方填充了很多对象,没问题。

然后用户可以在ddl中选择2个差异值,在点击按钮后显示在报告中,如下图所示:

同时,我的 component.ts 迭代器 curriculumArray 查找在ddl 中选择2个两个参数等对象并增加此值(图像的零值)。

for(const i of this.curriculumArray )
{
    if(this.combination1 == i.$name && this.combination2 == i.$age )
    {
    //some value incremented
    }else   
     if(this.combination1 == i.$name && this.combination2 == i.$gender )
    {
    //some value incremented
    }else   
     if(this.combination1 == i.$name && this.combination2 == i.$country )
    {
    //some value incremented
    }else 
     if(this.combination1 == i.$name && this.combination2 == i.$others)
    {
    //some value incremented
    }else 
    if(this.combination1 == i.$gender && this.combination2 == i.$name)
    {
    //filter curriculumArray
    }else 
    if(this.combination1 == i.$name && this.combination2 == i.$others)
    {
    //some value incremented
    }**...and other lot if's**  

我的问题:还有其他一些方法可以不需要使用批次if-else为每个组合? (性别+年龄),(性别+国家),(性别+宗教),(年龄+宗教),(宗教+国家),(国家+年龄)......

1 个答案:

答案 0 :(得分:1)

我会选择双for循环,但可能会将ES6与forEach一起使用:

curriculumArray.forEach((prop: any) => {
      curriculumArray.forEach((compareProp: any) => {
        if(this.combination1 === prop && this.combination2 === compareProp && prop !== compareProp) {
          // value increment
        }
      })
    });

在这两个循环中,我们将检查属性的组合并分配值。我没有更多信息,因此我不知道您将如何找出要增加的值,但您可以随时使用地图。

希望它有所帮助!