gpt4 book ai didi

javascript - 无法使用 Jquery/Javascript 根据键值对数组进行分组

转载 作者:行者123 更新时间:2023-12-03 00:13:47 25 4
gpt4 key购买 nike

我需要使用 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);

最佳答案

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

您在groups 结构中列出了此字段customer_name,但代码不使用它来区分记录。我想该代码最初并不是为此目的,并假设这样的字段在功能上依赖于列表中的第一个键(即,它假设相同的 lk_loan_account_id 将暗示相同的 customer_name ),但您的数据显然不是这种情况。

从业务 Angular 来看,贷款帐户 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);

关于javascript - 无法使用 Jquery/Javascript 根据键值对数组进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54605006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com