将sobject添加到列表javascript内的列表中

时间:2018-08-25 21:21:21

标签: javascript arrays arraylist salesforce

我有一个名为joinList的列表,其中包含一个sobject和一个sobject列表。看起来像这样

0:
   employeeRole:
      Employee__c:"a1n2C0000002SGqQAM"
      Id:"a1m2C000000cJaxQAE"
      Name:"Smith"
   employeeBlock:
        0:
            Id:"a1u2C00000011FQQAY"
            Name:"Week 2"
            Week_Number__c:2
            Total_Hours__c:4
        1:
            Id:"a1u2C00000011FQQAY"
            Name:"Week 5"
            Week_Number__c:5
            Total_Hours__c:6
1:{employeeRole: Array(0), employeeBlock: {…}}

我有一个整数字段,该整数字段使我获得了称为numberOfWeeksInput的星期数, 目标是星期数是5。

我想检查一下是否每个星期都存在一个employeeBlock,如果它不是每个星期都创建一个。

结果将是

我已经有第2周和第5周,我想为第1、3和4周创建一个。



更新答案

我有一个代码看起来像这样,我问是否有另一种方法可以不使用jquery来简化它

    var numberOfWeekList =[];
       for(var t = 0; t < numberOfWeeksInput;t++){
            numberOfWeekList.push((t+1));
        }
if( numberOfWeeksInput != null && numberOfWeeksInput != 0){
   for(var i = 0; i < joinList.length;i++){
        var empolyeeBlock = joinList[i].empolyeeBlock ;
        var existWeeks= [];
        if(empolyeeBlock !== null && empolyeeBlock.length !== 0){
         for(var t = 0; t < empolyeeBlock.length;t++){                        
             existWeeks.push(empolyeeBlock[t].Week_Number__c);
             }
         }
         var numberOfWeekUnassighned = numberOfWeekList.filter( ( el ) => !existWeeks.includes( el ) );
        if(numberOfWeekUnassighned !== null && numberOfWeekUnassighned.length !==0 && numberOfWeekUnassighned.length !== numberOfWeeksInput){
        for(var k = 0; k < numberOfWeekUnassighned.length;k++){
                            employeeBlock.push({
                                'sobjectType': 'EmployeeBlock__c',
                                'Name': 'Week '+(numberOfWeekUnassighned[k]),
                                'Week_Number__c':(numberOfWeekUnassighned[k]),
                                'Total_Hours__c':null
                            })
                        }
                    roleEstimateBlock.sort((a , b) => a.Week_Number__c - b.Week_Number__c);
                    }else if( employeeBlock.length <= numberOfWeeksInput){
                        for(var j = 0; j < numberOfWeeksInput;j++){
                            employeeBlock.push({
                                'sobjectType': 'EmployeeBlock__c',
                                'Name': 'Week '+(j+1),
                                'Week_Number__c':(j+1),
                                'Total_Hours__c':null
                            })
                        }
                    }
                }  
            }

0 个答案:

没有答案