0

I need to group my array values as per some key value using Jquery/Javascript but as per my code I could not get it. I am explaining my code below.

var dataArr=[
            {
              "login_id":"9937229853",
              "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":"",
              "coll_cat": "1stcoll",
              "reso_status": "1stres"
            },
            {
              "login_id":"9937229853",
              "allocated_to":"FIELD",
              "zone":"NORTH",
              "state":"DELHI",
              "location":"NEW DELHI",
              "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":"",
              "coll_cat": "2ndcoll",
              "reso_status": "2ndres"
            },
            {
              "login_id":"9937229867",
              "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":"",
              "coll_cat": "3rdcoll",
              "reso_status": "3rdres"
            },
            {
              "login_id":"9937229867",
              "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":"",
              "coll_cat": "4thcoll",
              "reso_status": "4thres"
            }
        ]

Here I need to group the all json array as per zone,state,location and login_id. My working code is given below.

 groups = [
          ['zone_list', 'zone'],
          ['state_list', 'state'],
          ['location_list', 'location'],
          ['task_list', 'login_id', 'front_end_manager_name', 'area_collection_manager', 'collection_manager'],
          ['loan_accounts_assigned', 'lk_loan_account_id', 'allocated_to', 'cl_contract_id', 'customer_name', 'customer_bank_name'],
          ['feedback_detail', 'coll_cat', 'reso_status']
      ],
      finalGroup = 'task_list',
      result = dataArr.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);

But my issue is I can group the whole value as per zone,state and location but as per login_id the grouping can not be made. I need to group as per zone,state,location and login_id. Please help me to resolve this issue.

Working snippet:

var dataArr=[{"login_id":"9937229853","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":"","coll_cat":"1stcoll","reso_status":"1stres"},{"login_id":"9937229853","allocated_to":"FIELD","zone":"NORTH","state":"DELHI","location":"NEW DELHI","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":"","coll_cat":"2ndcoll","reso_status":"2ndres"},{"login_id":"9937229867","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":"","coll_cat":"3rdcoll","reso_status":"3rdres"},{"login_id":"9937229867","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":"","coll_cat":"4thcoll","reso_status":"4thres"}],
    groups=[['zone_list','zone'],['state_list','state'],['location_list','location'],['task_list','login_id','front_end_manager_name','area_collection_manager','collection_manager'],['loan_accounts_assigned','lk_loan_account_id','allocated_to','cl_contract_id','customer_name','customer_bank_name'],['feedback_detail','coll_cat','reso_status']],
  finalGroup = 'task_list',
  result = dataArr.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);

9
  • 1
    Can you include the current and expected output at the question? Commented Feb 9, 2019 at 9:42
  • 1
    This is the exact code from here, but from an alt account? Commented Feb 9, 2019 at 9:53
  • 1
    I agree, seeing an example of how you want the output structured would help. I'm not clear on how you want things grouped. Commented Feb 9, 2019 at 9:56
  • 1
    please add what does not work. Commented Feb 9, 2019 at 9:56
  • 1
    @showdev : If you can check the output of console message it is expected output but in case of same login_id there should be 2 set of loan_accounts_assigned value but now on ly one is coming. Commented Feb 9, 2019 at 10:02

3 Answers 3

1

I see in your data that you have indeed the same login_id occurring twice in the input, and that the difference between those records is (amongst others) in the customer_name field.

You have this field customer_name listed in the groups structure, but the code does not use it to distinguish records. I suppose the code was not originally intended for that and assumed that such a field would be functionally dependent on the first key in the list (i.e. it assumed that the same lk_loan_account_id would imply the same customer_name), but this is evidently not the case in your data.

From a business point of view, it seems quite logical that a loan account id would uniquely identify the customer name, so maybe you should also check the validity of your input data.

If however, the input data is as expected, and it should differentiate on all fields listed in the groups structure, then replace the following line of code:

var temp = (t[group] = t[group] || []).find(p => o[keys[0]] === p[keys[0]]);

with:

var temp = (t[group] = t[group] || []).find(p => keys.every(key => o[key] === p[key]));

With this change the result would still have one grouped item for that duplicate login_id, but it would have two nested entries for the loan_accounts_assigned array property instead of just one.

Side note: you have a wrong field name in the groups structure: customer_bank_name should be bank_name_of_customer.

var dataArr=[
  {
    "login_id":"9937229853",
    "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":"",
    "coll_cat": "1stcoll",
    "reso_status": "1stres"
  },
  {
    "login_id":"9937229853",
    "allocated_to":"FIELD",
    "zone":"NORTH",
    "state":"DELHI",
    "location":"NEW DELHI",
    "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":"",
    "coll_cat": "2ndcoll",
    "reso_status": "2ndres"
  },
  {
    "login_id":"9937229867",
    "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":"",
    "coll_cat": "3rdcoll",
    "reso_status": "3rdres"
  },
  {
    "login_id":"9937229867",
    "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":"",
    "coll_cat": "4thcoll",
    "reso_status": "4thres"
  }
]

var groups = [
        ['zone_list', 'zone'],
        ['state_list', 'state'],
        ['location_list', 'location'],
        ['task_list', 'login_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'],
        ['feedback_detail', 'coll_cat', 'reso_status']
    ],
    finalGroup = 'task_list',
    result = dataArr.reduce((r, o) => {
        groups.reduce((t, [group, ...keys]) => {
            var temp = (t[group] = t[group] || []).find(p => keys.every(key => o[key] === p[key]));
            if (!temp) {
                temp = Object.assign({}, ...keys.map(k => ({ [k]: o[k] })));
                t[group].push(temp);
            }
            return temp;
        }, r);
        return r;
    }, {});

console.log(result);

Sign up to request clarification or add additional context in comments.

1 Comment

I think you've deduced the OP's desired output. Just for kicks, do you mind including a working example to see the output?
0

If you have to group based on specific key from Array of JSON with Easy way then I suggest using Javascript utility library underscore js or Lo-dash js. See below example that might solve your problem with readable code.

_.groupBy(dataArr,"login_id");
_.groupBy(dataArr,"state");
_.groupBy(dataArr,"zone");

var dataArr=[
            {
              "login_id":"9937229853",
              "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":"",
              "coll_cat": "1stcoll",
              "reso_status": "1stres"
            },
            {
              "login_id":"9937229853",
              "allocated_to":"FIELD",
              "zone":"NORTH",
              "state":"DELHI",
              "location":"NEW DELHI",
              "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":"",
              "coll_cat": "2ndcoll",
              "reso_status": "2ndres"
            },
            {
              "login_id":"9937229867",
              "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":"",
              "coll_cat": "3rdcoll",
              "reso_status": "3rdres"
            },
            {
              "login_id":"9937229867",
              "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":"",
              "coll_cat": "4thcoll",
              "reso_status": "4thres"
            }
        ];
        
       
       //usage _.groupBy(jsonArrayOfObjects,"key");
       var groupByLoginId=_.groupBy(dataArr,"login_id");
        console.log(groupByLoginId);
        
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Comments

0

Here is a simple groupBy function for this:

var dataArr=[
            {
              "login_id":"9937229853",
              "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":"",
              "coll_cat": "1stcoll",
              "reso_status": "1stres"
            },
            {
              "login_id":"9937229853",
              "allocated_to":"FIELD",
              "zone":"NORTH",
              "state":"DELHI",
              "location":"NEW DELHI",
              "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":"",
              "coll_cat": "2ndcoll",
              "reso_status": "2ndres"
            },
            {
              "login_id":"9937229867",
              "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":"",
              "coll_cat": "3rdcoll",
              "reso_status": "3rdres"
            },
            {
              "login_id":"9937229867",
              "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":"",
              "coll_cat": "4thcoll",
              "reso_status": "4thres"
            }
        ];

function groupBy(fields,data){
  var groups={};
  data.forEach(function(dataItem){
     var key=fields.reduce(function(result,fieldName){
        return (result && result+'&' || '')+fieldName+':'+(dataItem[fieldName])||'NULL';
     },'');
     if(!(key in groups))
         groups[key]=[];
     groups[key].push(dataItem);
    
  });
  return groups;
}

console.log(groupBy(['zone','state'],dataArr));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.