如何在打字稿中将一个哈希图复制到另一个哈希图?

时间:2020-07-02 13:59:35

标签: typescript

条件:复制时应使H1中已有的输入(hashmap1)具有与H2相同的更新(如果它们具有相同的密钥),并且应添加H1中不存在的输入。

在Java中,我们有putAll,但在打字稿中找不到类似的东西。 我尝试执行以下操作:

this.stmtTypeMap =stmtMap;

但这会覆盖 stmtTypeMap 的值。

我的代码是:

sumBacklog(tpbsDataArray: any) {
        //trust centers
        tpbsDataArray.forEach(element => {
            let stmtMap=new Map<String,Map<String,any>>();
            //category
            element.forEach(stmtDataArr => {
                //console.log(index);
                let stmtStatusMap = new Map<String,any>();
                //Expected, Received, Outstanding
                stmtDataArr.forEach((statusData, index) => {
                    //console.log(index);
                    //debugger;
                    let mySumObject = new mySumObjectClass().mysub;
                    let myTotalObject = new totalSumObjectClass().totalsub;
                    let data = new mySumObjectClass().mysub;
                    let totalData = new totalSumObjectClass().totalsub;

                    if(this.totalMap.has(statusData.STATUS)){
                        totalData = this.totalMap.get(statusData.STATUS);
                        this.sumTotalForExistingMapEnteries(myTotalObject, totalData, statusData);
                        this.totalMap.set(statusData.STATUS,myTotalObject);
                    }else{
                        this.sumTotalForNewMapEnteries(myTotalObject, statusData);
                        this.totalMap.set(statusData.STATUS,myTotalObject);
                    }
                    //console.log(index,stmtDataArr[index].CATEGORY);
                    if(this.stmtTypeMap.has(stmtDataArr[index].CATEGORY)){
                       let categoryData = this.stmtTypeMap.get(stmtDataArr[index].CATEGORY);
                       if(categoryData.has(statusData.STATUS)){
                        data = categoryData.get(statusData.STATUS);
                        this.sumColumnsForExistingMapEnteries(mySumObject, data, statusData);
                        
                        stmtStatusMap.set(statusData.STATUS,mySumObject);
                        stmtMap.set(stmtDataArr[index].CATEGORY, stmtStatusMap);
                       }
                       else{
                        this.sumColumnsForNewMapEnteries(mySumObject, statusData);
 
                        stmtStatusMap.set(statusData.STATUS,mySumObject);
                        stmtMap.set(stmtDataArr[index].CATEGORY, stmtStatusMap);
                       }
                    }
                    else{ 
                        this.sumColumnsForNewMapEnteries(mySumObject, statusData);
                        stmtStatusMap.set(statusData.STATUS,mySumObject);
                        stmtMap.set(stmtDataArr[index].CATEGORY, stmtStatusMap);
                    }
                })
            });
            this.stmtTypeMap =stmtMap;
        });
        return this.stmtTypeMap;
    }

1 个答案:

答案 0 :(得分:0)

如果您是指对象,则可以像这样合并它们,

let map1 = {a:1, b:2};
let map2 = {c:3, b:4};
let mergedMap = {...map1, ...map2};

但是,如果您指的是Map对象,则类似的事情会起作用

let firstMap: Map<string, number> = new Map([["a", 1], ["b", 2]]);
let secondMap: Map<string, number> = new Map([["c", 3], ["b", 4]]);

let mergedMap: Map<string, number> = new Map([...Array.from(firstMap.entries()), ...Array.from(secondMap.entries())]);