JS 配列は、同じキー値を持つ 2 つの json を 1 つの配列 double for loop にマージします

要件: バックグラウンドによって返される 2 つの配列 json 文字列があります.同じ対応する deptIdに従って、 2 番目の json の deptName を最初の json にマージして、新しい配列を形成します
ここに画像の説明を挿入

          // 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)

おすすめ

転載: blog.csdn.net/Start_Simple/article/details/130298841