如何使用JavaScript按键和值映射对象?

时间:2019-01-18 09:06:07

标签: javascript arrays

我想通过对象键对对象数组进行分组,然后根据该分组创建一个新的对象数组。我在下面显示我的对象。

var oldArr=[
            {
          "_id":"5c407834953d7f420d56f866",
          "allocated_to":"FIELD",
          "zone":"NORTH",
          "state":"DELHI",
          "location":"NEW DELHI",
          "customer_name":"REET INFOTECH",
          "bank_name_of_customer":"YES BANK",
          "cl_contract_id":"LAI-00016881",
          "lk_loan_account_id":"LK0000015094",
          "front_end_manager_name":"SONAL",
          "area_collection_manager":"ASHIS JENA",
          "installment_date":"",
          "collection_manager":"",
        },
        {
          "_id":"5c407834953d7f420d56f867",
          "allocated_to":"FIELD",
          "zone":"NORTH",
          "state":"DELHI",
          "location":"Sree Nagar",
          "customer_name":"REET",
          "bank_name_of_customer":"Corporate BANK",
          "cl_contract_id":"LAI-00016881",
          "lk_loan_account_id":"LK0000015094",
          "front_end_manager_name":"SONAL",
          "area_collection_manager":"ASHIS JENA",
          "installment_date":"",
          "collection_manager":"",
        },
        {
          "_id":"5c407834953d7f420d56f868",
          "allocated_to":"FIELD",
          "zone":"EAST",
          "state":"Odisha",
          "location":"Bhubaneswar",
          "customer_name":"REET",
          "bank_name_of_customer":"PNB BANK",
          "cl_contract_id":"LAI-00016881",
          "lk_loan_account_id":"LK0000015094",
          "front_end_manager_name":"SONAL",
          "area_collection_manager":"ASHIS JENA",
          "installment_date":"",
          "collection_manager":"",
        },
        {
          "_id":"5c407834953d7f420d56f890",
          "allocated_to":"FIELD",
          "zone":"EAST",
          "state":"Assam",
          "location":"Gawhati",
          "customer_name":"REET",
          "bank_name_of_customer":"SBI BANK",
          "cl_contract_id":"LAI-00016881",
          "lk_loan_account_id":"LK0000015094",
          "front_end_manager_name":"SONAL",
          "area_collection_manager":"ASHIS JENA",
          "installment_date":"",
          "collection_manager":"",
        }
]

我需要制作一个新数组,该数组应按zone,state,location分组,格式如下。

 newdata={
          zone_list: [{
            zone: NORTH,
            state_list: [{    
              state: DELHI,
              location_list: [{
                location: NEW DELHI,
                task_list: [{
                  user_pkId: 5c407834953d7f420d56f866,
                  front_end_manager_name: SONAL,
                  collection_manager: "",
                  area_collection_manager: ASHIS JENA,
                  loan_accounts_assigned: [{
                    allocated_to: FIELD,
                    lk_loan_account_id: LK0000015094,
                    cl_contract_id: LAI-00016881,
                    customer_name: REET INFOTECH,
                    customer_bank_name: YES BANK,
                  }]
                }]
              }]
            },{
              state: JK,
              location_list: [{
                location: Sree Nagar,
                task_list: [{
                  user_pkId: 5c407834953d7f420d56f867,
                  front_end_manager_name: SONAL,
                  collection_manager: "",
                  area_collection_manager: ASHIS JENA,
                  loan_accounts_assigned: [{
                    allocated_to: FIELD,
                    lk_loan_account_id: LK0000015094,
                    cl_contract_id: LAI-00016881,
                    customer_name: REET INFOTECH,
                    customer_bank_name: Corporate BANK,
                  }]
                }]
              }]
            }]
          },{
            zone: EAST,
            state_list: [{    
              state: Odisha,
              location_list: [{
                location: Bhubaneswar,
                task_list: [{
                  user_pkId: 5c407834953d7f420d56f868,
                  front_end_manager_name: SONAL,
                  collection_manager: "",
                  area_collection_manager: ASHIS JENA,
                  loan_accounts_assigned: [{
                    allocated_to: FIELD,
                    lk_loan_account_id: LK0000015094,
                    cl_contract_id: LAI-00016881,
                    customer_name: REET INFOTECH,
                    customer_bank_name: SBI BANK,
                  }]
                }]
              }]
            }]
          }]
        }

我在下面提供我的代码。

var zonelist=[];
var statelist=[];
 var locationlist=[];
var tasklist=[];
var loanarr=[];
var finalArr=[];
for(var i=0;i<dataArr.length;i++){
          if (zonelist.length > 0) {
            var hasZone=false;
            var zoneindex='';
            for(var j=0;j<zonelist.length;j++){
              if (dataArr[i]['zone']==zonelist[j]['zone']) {
                hasZone=true;
                zoneindex=j;
                break;
              }
            }
            if (hasZone==true) {
              var hasState=false;
              var stateindex='';
              for(var k=0;k<zonelist[zoneindex]['state_list'].length;k++){
                if (dataArr[i]['state']==zonelist[zoneindex]['state_list'][k]['state']) {
                  hasState=true;
                  stateindex=k;
                }
              }
              if (hasState==true) {
                var haslocation=false;
                var locindex='';
                for(var l=0;l<zonelist[zoneindex]['state_list'][stateindex]['location_list'].length;l++){
                  if (dataArr[i]['location']==zonelist[zoneindex]['state_list'][stateindex]['location_list'][l]['location']) {
                    haslocation=true;
                    locindex=l;
                  }
                }
                if (haslocation==true) {
                  var hasUser=false;
                  var userindex='';

                }else{
                  var dataloan={'allocated_to':dataArr[i]['allocated_to'],'lk_loan_account_id':dataArr[i]['lk_loan_account_id'],'cl_contract_id':dataArr[i]['cl_contract_id'],'customer_name':dataArr[i]['customer_name'],'customer_bank_name':dataArr[i]['bank_name_of_customer']};
                  loanarr.push(datatask);
                  var datatask={'user_pkId':dataArr[i]['_id'],'front_end_manager_name':dataArr[i]['front_end_manager_name'],'collection_manager':dataArr[i]['collection_manager'],'area_collection_manager':dataArr[i]['area_collection_manager'],'loan_accounts_assigned':loanarr};
                  tasklist.push(datatask);
                  var dataloc={'location':dataArr[i]['location'],'task_list':tasklist};
                  locationlist.push(dataloc);
                }
              }else{
                var dataloan={'allocated_to':dataArr[i]['allocated_to'],'lk_loan_account_id':dataArr[i]['lk_loan_account_id'],'cl_contract_id':dataArr[i]['cl_contract_id'],'customer_name':dataArr[i]['customer_name'],'customer_bank_name':dataArr[i]['bank_name_of_customer']};
                loanarr.push(datatask);
                var datatask={'user_pkId':dataArr[i]['_id'],'front_end_manager_name':dataArr[i]['front_end_manager_name'],'collection_manager':dataArr[i]['collection_manager'],'area_collection_manager':dataArr[i]['area_collection_manager'],'loan_accounts_assigned':loanarr};
                tasklist.push(datatask);
                var dataloc={'location':dataArr[i]['location'],'task_list':tasklist};
                locationlist.push(dataloc);
                var datastate={'state':dataArr[i]['state'],'location_list':locationlist};
                statelist.push(datastate);
              }
            }else{
              var dataloan={'allocated_to':dataArr[i]['allocated_to'],'lk_loan_account_id':dataArr[i]['lk_loan_account_id'],'cl_contract_id':dataArr[i]['cl_contract_id'],'customer_name':dataArr[i]['customer_name'],'customer_bank_name':dataArr[i]['bank_name_of_customer']};
                loanarr.push(datatask);
              var datatask={'user_pkId':dataArr[i]['_id'],'front_end_manager_name':dataArr[i]['front_end_manager_name'],'collection_manager':dataArr[i]['collection_manager'],'area_collection_manager':dataArr[i]['area_collection_manager'],'loan_accounts_assigned':loanarr};
                tasklist.push(datatask);
              var dataloc={'location':dataArr[i]['location'],'task_list':tasklist};
              locationlist.push(dataloc);
              var datastate={'state':dataArr[i]['state'],'location_list':locationlist};
              statelist.push(datastate);
              var datazone={'zone':dataArr[i]['zone'],'state_list':statelist};
              zonelist.push(datazone);
            }
          }else{
            var dataloan={'allocated_to':dataArr[i]['allocated_to'],'lk_loan_account_id':dataArr[i]['lk_loan_account_id'],'cl_contract_id':dataArr[i]['cl_contract_id'],'customer_name':dataArr[i]['customer_name'],'customer_bank_name':dataArr[i]['bank_name_of_customer']};
            loanarr.push(dataloan);
            var datatask={'user_pkId':dataArr[i]['_id'],'front_end_manager_name':dataArr[i]['front_end_manager_name'],'collection_manager':dataArr[i]['collection_manager'],'area_collection_manager':dataArr[i]['area_collection_manager'],'loan_accounts_assigned':loanarr};
            tasklist.push(datatask);
            var dataloc={'location':dataArr[i]['location'],'task_list':tasklist};
            locationlist.push(dataloc);
            var datastate={'state':dataArr[i]['state'],'location_list':locationlist};
            statelist.push(datastate);
            var datazone={'zone':dataArr[i]['zone'],'state_list':statelist};
            zonelist.push(datazone);
          }
        }
        var findata={'zone_list':zonelist};
        console.log('finaldata',findata);

但是上面的代码没有给出预期的输出。

2 个答案:

答案 0 :(得分:1)

您可以为所需的组和相关键采用数组,并采用迭代和递归的方法。

seq_cnt
char[5+1]

答案 1 :(得分:0)

下面的代码不能给您完整的正确结果,但是您应该更好地了解如何做。 .reduce()的引用为:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

sbt "it:testOnly test.Spec"
sbt "IntegrationTest / testOnly test.Spec"