无法使用Jquery / Javascript

时间:2019-02-09 09:38:51

标签: javascript jquery

我需要使用Jquery / Javascript根据某个键值对数组值进行分组,但是根据我的代码,我无法获取它。我在下面解释我的代码。

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

在这里,我需要根据zone,state,location and login_id对所有json数组进行分组。我的工作代码如下。

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

但是我的问题是我可以按区域,州和位置对整个值进行分组,但是根据login_id无法进行分组。我需要按照zone,state,location and login_id进行分组。请帮助我解决此问题。

有效代码段

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

3 个答案:

答案 0 :(得分:1)

我在您的数据中看到,您确实在输入中两次出现相同的login_id,并且这些记录之间的差异(与其他记录相比)在customer_name字段中。

您在customer_name结构中列出了该字段groups,但是代码没有使用它来区分记录。我想代码最初不是为此目的而设计的,并且假定该字段在功能上将取决于列表中的第一个键(即,假定相同的lk_loan_account_id意味着相同的customer_name),但这显然不是您的数据。

从业务角度来看,借贷帐户ID可以唯一标识客户名称似乎是合乎逻辑的,因此也许您还应该检查输入数据的有效性。

但是,如果输入数据符合预期,并且应该在groups结构中列出的所有字段上进行区分,则替换下面的代码行:

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

具有:

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

进行此更改后,结果仍将为该重复的login_id具有一个分组项,但对于loan_accounts_assigned数组属性,它将具有两个嵌套条目,而不仅仅是一个。

旁注:groups结构中的字段名称错误:customer_bank_name应该为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);

答案 1 :(得分:0)

如果您必须使用Easy方法基于JSON数组中的特定键进行分组,那么我建议使用Javascript实用程序库下划线js或Lo-dash js 。请参见下面的示例,该示例可能会通过可读代码解决您的问题。

_.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>

答案 2 :(得分:0)

这是一个简单的groupBy函数:

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