Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

arrays - How to map an object as per key and value using JavaScript?

I want to group an array of objects by an object key then create a new array of objects based on the grouping. I am showing my object below.

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":"",
        }
]

I need to make new array which should be group by zone,state,location and format is given below.

 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,
                  }]
                }]
              }]
            }]
          }]
        }

I am providing my code below.

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

But above code did not give the expected output.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could take an array for the wanted groups and related keys and take an iterative and recursive approach.

var data = [{ _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: "" }],
    groups = [
        ['zone_list', 'zone'],
        ['state_list', 'state'],
        ['location_list', 'location'],
        ['task_list', '_id', 'front_end_manager_name', 'area_collection_manager', 'collection_manager'],
        ['loan_accounts_assigned', 'lk_loan_account_id', 'allocated_to', 'cl_contract_id', 'customer_name', 'bank_name_of_customer']
    ],
    result = data.reduce((r, o) => {
        groups.reduce((t, [group, ...keys]) => {
            var temp = (t[group] = t[group] || []).find(p => o[keys[0]] === p[keys[0]]);
            if (!temp) {
                temp = Object.assign({}, ...keys.map(k => ({ [k]: o[k] })));
                t[group].push(temp);
            }
            return temp;
        }, r);
        return r;
    }, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...