JS array merges two json with the same key value into one array double for loop

Requirement: There are two array json strings returned by the background. According to the same corresponding deptId, merge the deptName in the second json into the first json to form a new array
insert image description here

          // this.manageList 为json1
          // this.getdeptIdList 为json2
          var listAllmanage = [], getIdIndex = {
    
    };
            for (var i = 0; i < this.getdeptIdList.length; i++) {
    
    
                for (var j = 0; j < this.manageList.length; j++) {
    
    
                    if (this.getdeptIdList[i].deptId == this.manageList[j].deptId) {
    
    
                        var item
                        if (getIdIndex[this.getdeptIdList[i].deptId] == undefined) {
    
    
                            getIdIndex[this.getdeptIdList[i].deptId] = listAllmanage.length;
                            item = {
    
    };
                            for (var attr in this.getdeptIdList[i]) item[attr] = this.getdeptIdList[i][attr];
                            listAllmanage[getIdIndex[this.getdeptIdList[i].deptId]] = item;

                        } else item = listAllmanage[getIdIndex[this.getdeptIdList[i].deptId]];
                        for (var attr in this.manageList[j]) item[attr] = this.manageList[j][attr];
                    }
                }
            }
            console.log(listAllmanage)

Guess you like

Origin blog.csdn.net/Start_Simple/article/details/130298841