如何在angularJS中组合两个对象

时间:2016-06-10 10:47:06

标签: jquery angularjs

我有两个请求,一个获取员工信息,另一个获得津贴信息。这两者与empID相关。如何将这两个请求组合在angularJS中,如第3级所示?

第1级:这是员工信息

[
  {
    "empID": "PC145",
    "fullName": "Lamin   L  Janneh",
    "Job": "Software Developer",
    "Dept": "Tech",
    "bank": "Trust Bank Limited",
    "Account": "789653214",
    "tin": "785421369"
  },
  {
    "empID": "PC676",
    "fullName": "Agie   A  Fatou",
    "Job": "Software Developer",
    "Dept": "Tech",
    "bank": "Trust Bank Limited",
    "Account": "8521479",
    "tin": "7895214"
  }
]

第2级:这是员工津贴信息对象

[
  {
    "empID": "PC676",
    "name": "House Allowance",
    "amount": "700.00"
  },
  {
    "empID": "PC676",
    "name": "House Allowance",
    "amount": "700.00"
  },
  {
    "empID": "PC145",
    "name": "Credit Allowance",
    "amount": "1000.00"
  },
  {
    "empID": "PC145",
    "name": "House Allowance",
    "amount": "450.00"
  }
]

等级2:我如何组合这两个对象?

[
  {
    "empID": "PC145",
    "fullName": "Lamin   L  Janneh",
    "Job": "Software Developer",
    "Dept": "Tech",
    "bank": "Trust Bank Limited",
    "Account": "789653214",
    "tin": "785421369",
    "Allowance":[
        {
            "empID": "PC145",
            "name": "Credit Allowance",
            "amount": "1000.00"
        },{
        "empID": "PC145",
        "name": "Credit Allowance",
        "amount": "1000.00"
      }]
    },
    {
        "empID": "PC676",
        "fullName": "Agie   A  Fatou",
        "Job": "Software Developer",
        "Dept": "Tech",
        "bank": "Trust Bank Limited",
        "Account": "8521479",
        "tin": "7895214",
        "Allowance" : 
            [{
                "empID": "PC676",
                "name": "House Allowance",
                "amount": "700.00"
              },
              {
                "empID": "PC676",
                "name": "House Allowance",
                "amount": "700.00"
             }]
 }
]

提前致谢!!!

3 个答案:

答案 0 :(得分:0)

只有在收到 两个 数据集时才这样做很重要,所以请务必在正确的位置调用以下函数:< / p>

function mergeAllowances(employee, allowance) {
    // 1. create map for level 1 and add allowance property

    var dict = employee.reduce(function (dict, emp) {
        emp.allowance = [];
        dict[emp.empID] = emp;
        return dict;
    }, {});


    // 2. Populate allowance property

    allowance.forEach(function (entry) {
        dict[entry.empID].allowance.push(entry);
    });

    return employee;
}

// Sample data. Instead you would use $scope.employeee and $scope.Allowance
var employee = [
  {
    "empID": "PC145",
    "fullName": "Lamin   L  Janneh",
    "Job": "Software Developer",
    "Dept": "Tech",
    "bank": "Trust Bank Limited",
    "Account": "789653214",
    "tin": "785421369"
  },
  {
    "empID": "PC676",
    "fullName": "Agie   A  Fatou",
    "Job": "Software Developer",
    "Dept": "Tech",
    "bank": "Trust Bank Limited",
    "Account": "8521479",
    "tin": "7895214"
  }
];

var allowance = [
  {
    "empID": "PC676",
    "name": "House Allowance",
    "amount": "700.00"
  },
  {
    "empID": "PC676",
    "name": "House Allowance",
    "amount": "700.00"
  },
  {
    "empID": "PC145",
    "name": "Credit Allowance",
    "amount": "1000.00"
  },
  {
    "empID": "PC145",
    "name": "House Allowance",
    "amount": "450.00"
  }
];

// Apply merge to employee
mergeAllowances(employee, allowance);
// Output result
console.log(employee);

请注意,我不会使用大写写Allowance:使用camelCase代替allowance。初始大写通常用于类/构造函数。

答案 1 :(得分:0)

你可以这样做

其中

var parent= [
  {
    "empID": "PC145",
    "fullName": "Lamin   L  Janneh",
    "Job": "Software Developer",
    "Dept": "Tech",
    "bank": "Trust Bank Limited",
    "Account": "789653214",
    "tin": "785421369"
  },
  {
    "empID": "PC676",
    "fullName": "Agie   A  Fatou",
    "Job": "Software Developer",
    "Dept": "Tech",
    "bank": "Trust Bank Limited",
    "Account": "8521479",
    "tin": "7895214"
  }
];

var child= [
  {
    "empID": "PC676",
    "name": "House Allowance",
    "amount": "700.00"
  },
  {
    "empID": "PC676",
    "name": "House Allowance",
    "amount": "700.00"
  },
  {
    "empID": "PC145",
    "name": "Credit Allowance",
    "amount": "1000.00"
  },
  {
    "empID": "PC145",
    "name": "House Allowance",
    "amount": "450.00"
  }
];

您的功能可能如下所示

for (i = 0; i < parent.length; i++) 
{ 
    parent[i].Allowance = [];  
    for(var j = 0; j< child.length; j++) { 
        if(parent[i].empID == child[j].empID) {
        parent[i].Allowance.push(child[j])
       }
    }   
} 

答案 2 :(得分:0)

var master = [

{       “empID”:“PC145”,       “fullName”:“Lamin L Janneh”,       “工作”:“软件开发人员”,       “部门”:“技术”,       “银行”:“信托银行有限公司”,       “帐户”:“789653214”,       “tin”:“785421369”   },   {       “empID”:“PC676”,       “fullName”:“Agie A Fatou”,       “工作”:“软件开发人员”,       “部门”:“技术”,       “银行”:“信托银行有限公司”,       “帐户”:“8521479”,       “tin”:“7895214”   }     ];

var detail =  [

{       “empID”:“PC676”,       “名称”:“房屋津贴”,       “金额”:“700.00”   },   {       “empID”:“PC676”,       “名称”:“房屋津贴”,       “金额”:“700.00”   },   {       “empID”:“PC145”,       “名称”:“信贷津贴”,       “金额”:“1000.00”   },   {       “empID”:“PC145”,       “名称”:“房屋津贴”,       “金额”:“450.00”   }     ];

for (var inxMaster = 0; inxMaster < master.length; inxMaster++)
{
   master[inxMaster].Allowance = [];
    for (var inxDetail = 0; inxDetail < detail.length; inxDetail++) {
        if (master[inxMaster].empID === detail[inxDetail].empID)
        {
            master[inxMaster].Allowance.push(detail[inxDetail]);
        }
    }
}