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