gpt4 book ai didi

javascript - 按包含对象的数组中的日期键排序

转载 作者:行者123 更新时间:2023-11-28 03:24:23 25 4
gpt4 key购买 nike

我有一个包含多个数组的数组,每个数组包含多个对象。

我需要使用键对整个数组(json_act_super)进行排序:Created(日期按升序排列)

我尝试展平数组,然后使用以下函数和各种其他方法进行排序,但没有成功

例如:

 var yourFlattenedArray = flatten(json_act_super);
new_arr = new_arr.sort(yourFlattenedArray );

function flatten(array) {
return !Array.isArray(array) ? array : [].concat.apply([],
array.map(flatten));
}
function custom_sort(a, b) {
return new Date(a.Created).getTime() - new
Date(b.Created).getTime();
}

var json_act_super= [[{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"12/1/2018","StatusValue":"Finished","ActivityText":null,"Created":"11/1/2018","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Marius Jaselskis","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"15/1/2018","StatusValue":"Finished","ActivityText":null,"Created":"12/1/2018","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Marius Jaselskis","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"24/1/2018","StatusValue":"Finished","ActivityText":null,"Created":"23/1/2018","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Laura Ivanauskaite","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"4/5/2018","StatusValue":"Finished","ActivityText":null,"Created":"3/5/2018","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Line Schøtt Jensen","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"15/5/2018","StatusValue":"Finished","ActivityText":null,"Created":"14/5/2018","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Kristina Ciegyte","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"8/3/2019","StatusValue":"Finished","ActivityText":null,"Created":"7/3/2019","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Ana Moskaliova","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"25/3/2019","StatusValue":"Finished","ActivityText":null,"Created":"22/3/2019","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Brigita Kapcinske","ActivityType":"ESTHA_HandleClaim1P8"}],[{"Title":"Claim","CaseID":1575274,"DueDate":"15/10/2019","StatusValue":"Finished","ActivityText":null,"Created":"9/10/2019","Path":"/sites/estate/Lists/x Claim","ChangedBy":"Laura Kvedariene","ActivityType":"ESTHA_HandleClaim2P8"}],[{"Title":"636619761964313943","CaseID":1575274,"DueDate":"22/5/2018","StatusValue":"Finished","ActivityText":null,"Created":"15/5/2018","Path":"/sites/estate/Lists/x Mixed","ChangedBy":"Line Schøtt Jensen","ActivityType":"ESTHA_HandleProperty2P8"}],[{"Title":"636878973199308438","CaseID":1575274,"DueDate":"2/4/2019","StatusValue":"Finished","ActivityText":null,"Created":"11/3/2019","Path":"/sites/estate/Lists/x Mixed Follow Up DK","ChangedBy":"Helle Vinther Lorenzen","ActivityType":"ESTHA_FollowUponProperty2P8"}],[{"Title":"636514502410561268","CaseID":1575274,"DueDate":"16/1/2018","StatusValue":"Finished","ActivityText":null,"Created":"13/1/2018","Path":"/sites/estate/Lists/x Mixed Urgent","ChangedBy":"Marianne Rohde","ActivityType":"ESTHA_HandlePropertyUrgent2P8"},{"Title":"636514502429795141","CaseID":1575274,"DueDate":"16/1/2018","StatusValue":"Finished","ActivityText":null,"Created":"13/1/2018","Path":"/sites/estate/Lists/x Mixed Urgent","ChangedBy":"Marianne Rohde","ActivityType":"ESTHA_HandlePropertyUrgent2P8"},{"Title":"636876411401152914","CaseID":1575274,"DueDate":"11/3/2019","StatusValue":"Finished","ActivityText":null,"Created":"8/3/2019","Path":"/sites/estate/Lists/x Mixed Urgent","ChangedBy":"Kim Boysen","ActivityType":"ESTHA_HandlePropertyUrgent2P8"},{"Title":"636891115107285634","CaseID":1575274,"DueDate":"26/3/2019","StatusValue":"Finished","ActivityText":null,"Created":"25/3/2019","Path":"/sites/estate/Lists/x Mixed Urgent","ChangedBy":"Helle Vinther Lorenzen","ActivityType":"ESTHA_HandlePropertyUrgent2P8"}],[{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"1/2/2018","StatusValue":"Finished","ActivityText":null,"Created":"4/1/2018","Path":"/sites/estate/Lists/Anmeld krav","ChangedBy":"Kristina Ciegyte","ActivityType":"ESTHA_SubmitClaimP8"}],[{"Title":"636793139973643792","CaseID":1575274,"DueDate":"7/1/2019","StatusValue":"Finished","ActivityText":null,"Created":"2/12/2018","Path":"/sites/estate/Lists/Opflgning 6 mneder","ChangedBy":"RPA task 1750 PEH 3860 Closing manual remind 6 months follow up activities","ActivityType":"Opfølgning - 6 måneder"}]]

扁平化和排序不起作用。请帮助我如何按时间顺序对整个内容进行排序(按创建日期排序)

最佳答案

您需要将数组展平(可以使用内置的 flatten 方法),然后使用 sort 方法按升序排序:

var json_act_super= [[{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"12/1/2018","StatusValue":"Finished","ActivityText":null,"Created":"11/1/2018","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Marius Jaselskis","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"15/1/2018","StatusValue":"Finished","ActivityText":null,"Created":"12/1/2018","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Marius Jaselskis","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"24/1/2018","StatusValue":"Finished","ActivityText":null,"Created":"23/1/2018","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Laura Ivanauskaite","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"4/5/2018","StatusValue":"Finished","ActivityText":null,"Created":"3/5/2018","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Line Schøtt Jensen","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"15/5/2018","StatusValue":"Finished","ActivityText":null,"Created":"14/5/2018","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Kristina Ciegyte","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"8/3/2019","StatusValue":"Finished","ActivityText":null,"Created":"7/3/2019","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Ana Moskaliova","ActivityType":"ESTHA_HandleClaim1P8"},{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"25/3/2019","StatusValue":"Finished","ActivityText":null,"Created":"22/3/2019","Path":"/sites/estate/Lists/Handle Mail 1","ChangedBy":"Brigita Kapcinske","ActivityType":"ESTHA_HandleClaim1P8"}],[{"Title":"Claim","CaseID":1575274,"DueDate":"15/10/2019","StatusValue":"Finished","ActivityText":null,"Created":"9/10/2019","Path":"/sites/estate/Lists/x Claim","ChangedBy":"Laura Kvedariene","ActivityType":"ESTHA_HandleClaim2P8"}],[{"Title":"636619761964313943","CaseID":1575274,"DueDate":"22/5/2018","StatusValue":"Finished","ActivityText":null,"Created":"15/5/2018","Path":"/sites/estate/Lists/x Mixed","ChangedBy":"Line Schøtt Jensen","ActivityType":"ESTHA_HandleProperty2P8"}],[{"Title":"636878973199308438","CaseID":1575274,"DueDate":"2/4/2019","StatusValue":"Finished","ActivityText":null,"Created":"11/3/2019","Path":"/sites/estate/Lists/x Mixed Follow Up DK","ChangedBy":"Helle Vinther Lorenzen","ActivityType":"ESTHA_FollowUponProperty2P8"}],[{"Title":"636514502410561268","CaseID":1575274,"DueDate":"16/1/2018","StatusValue":"Finished","ActivityText":null,"Created":"13/1/2018","Path":"/sites/estate/Lists/x Mixed Urgent","ChangedBy":"Marianne Rohde","ActivityType":"ESTHA_HandlePropertyUrgent2P8"},{"Title":"636514502429795141","CaseID":1575274,"DueDate":"16/1/2018","StatusValue":"Finished","ActivityText":null,"Created":"13/1/2018","Path":"/sites/estate/Lists/x Mixed Urgent","ChangedBy":"Marianne Rohde","ActivityType":"ESTHA_HandlePropertyUrgent2P8"},{"Title":"636876411401152914","CaseID":1575274,"DueDate":"11/3/2019","StatusValue":"Finished","ActivityText":null,"Created":"8/3/2019","Path":"/sites/estate/Lists/x Mixed Urgent","ChangedBy":"Kim Boysen","ActivityType":"ESTHA_HandlePropertyUrgent2P8"},{"Title":"636891115107285634","CaseID":1575274,"DueDate":"26/3/2019","StatusValue":"Finished","ActivityText":null,"Created":"25/3/2019","Path":"/sites/estate/Lists/x Mixed Urgent","ChangedBy":"Helle Vinther Lorenzen","ActivityType":"ESTHA_HandlePropertyUrgent2P8"}],[{"Title":"1008320604ELSEFALSIG_1008320604","CaseID":1575274,"DueDate":"1/2/2018","StatusValue":"Finished","ActivityText":null,"Created":"4/1/2018","Path":"/sites/estate/Lists/Anmeld krav","ChangedBy":"Kristina Ciegyte","ActivityType":"ESTHA_SubmitClaimP8"}],[{"Title":"636793139973643792","CaseID":1575274,"DueDate":"7/1/2019","StatusValue":"Finished","ActivityText":null,"Created":"2/12/2018","Path":"/sites/estate/Lists/Opflgning  6 mneder","ChangedBy":"RPA task 1750 PEH 3860 Closing manual remind 6 months follow up activities","ActivityType":"Opfølgning - 6 måneder"}]];

const flattedArray = json_act_super.flat(2);

flattedArray.sort((a, b) => {
a = a['Created'].split('/');
b = b['Created'].split('/');
return a[2] - b[2] || a[1] - b[1] || a[0] - b[0];
});

console.log(JSON.stringify(flattedArray, null, 4));

关于javascript - 按包含对象的数组中的日期键排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58785723/

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