简体   繁体   English

javascript forEach on数组/ json数据,但按最新日期排序?

[英]javascript forEach on array / json data but sortby latest date?

Is it possible to modify the following so that on the foreach it would reverse so that latest date of data is first rather than oldest to newest? 是否可以修改以下内容,以便在foreach上将其反转,以使最新数据的日期排在第一位,而不是从最早到最新?

 var json = { "TrackingRecord": { "Authorised": "Authorised(5.77.48.131)", "DeliveryAddress": { "CompanyName": "JAMES DERICK", "Address1": "6", "Address2": "LIBER HOUSE", "Address3": "OLYMPIAN", "Town": "YORK", "Postcode": "YO10 3UF", "ContactName": "JAMES DERICK", "ContactTelephone": "7507346318" }, "CollectionAddress": { "CompanyName": "AMBIENT LOUNGE LTD", "Address1": "UNIT 3 LONG HEDGE LANE INDUSTR", "Address2": "BOTTESFORD", "Address3": { }, "Town": "NOTTINGHAM", "Postcode": "NG13 0BF", "ContactName": "SARAH KIRBY", "ContactTelephone": "07879 442266074" }, "ConsignmentInformation": { "Pieces": "1", "Pallets": "0", "Weight": "10", "Service": "Priority 1", "DeliveryDate": "2016-02-29T00:00:00", "ItemsDelivered": "1", "ConsignmentRef": "2838", "SpecialInstructions": "JAMES DERICK 7507346318 {JAMES\
    
    
    
      14075@GMAIL.COM}\ 
     
       ", "AdditionalReferencesInformation": { "AdditionalReferences": { "Reference": "2838" } } }, "MovementInformation": { "Movement": [{ "MovementDate": "2016-02-25T00:00:00", "MovementTime": "0001-01-01T10:00:04", "Description": "Created By EZEEWEB", "DeliveryDepot": "Leeds", "Round": "019", "DeliveryDate": "2016-02-26T00:00:00", "PackagesReceived": "0", "PackagesDelivered": "0" }, { "MovementDate": "2016-02-26T00:00:00", "MovementTime": "0001-01-01T07:11:53", "Description": "Out to deliver", "DeliveryDepot": "Leeds", "Round": "019", "DeliveryDate": "2016-02-26T00:00:00", "PackagesReceived": "1", "PackagesDelivered": "0" }, { "MovementDate": "2016-02-26T00:00:00", "MovementTime": "0001-01-01T11:00:53", "Description": "Failed - Other reason", "DeliveryDepot": "Leeds", "Round": "019", "DeliveryDate": "2016-02-29T00:00:00", "PackagesReceived": "1", "PackagesDelivered": "0" }, { "MovementDate": "2016-02-27T00:00:00", "MovementTime": "0001-01-01T05:59:32", "Description": "Out to deliver", "DeliveryDepot": "Leeds", "Round": "019", "DeliveryDate": "2016-02-29T00:00:00", "PackagesReceived": "1", "PackagesDelivered": "0" }, { "MovementDate": "2016-02-29T00:00:00", "MovementTime": "0001-01-01T10:55:43", "Description": "Delivered", "DeliveryDepot": "Leeds", "Round": "019", "DeliveryDate": "2016-02-29T00:00:00", "PackagesReceived": "1", "PackagesDelivered": "1" }] }, "TimedInformation": { "TimedDelivery": { "Signature": "DERICK", "SignatureDate": "2016-02-29T00:00:00", "SignatureTime": "0001-01-01T10:55:00" } }, "ScanInformation": { "Scan": [{ "PieceID": "148426702251072001", "Description": "Auto Inbound Scan ()", "Depot": "Newark", "ScanDate": "2016-02-25T00:00:00", "ScanTime": "0001-01-01T17:12:01", "ScannedBy": "NWK CONVYR" }, { "PieceID": "148426702251072001", "Description": "Auto Inbound Scan ()", "Depot": "Leeds", "ScanDate": "2016-02-26T00:00:00", "ScanTime": "0001-01-01T02:22:08", "ScannedBy": "LDS CONVYR" }, { "PieceID": "148426702251072001", "Description": "Load C & D (019)", "Depot": "Leeds", "ScanDate": "2016-02-26T00:00:00", "ScanTime": "0001-01-01T03:37:45", "ScannedBy": "CJONES" }, { "PieceID": "148426702251072001", "Description": "Load C & D (019)", "Depot": "Leeds", "ScanDate": "2016-02-26T00:00:00", "ScanTime": "0001-01-01T23:43:22", "ScannedBy": "CJONES" }] }, "ImageInformation": { "PODImage": { "URL": "http:\\/\\/www.tpeweb.co.uk\\/ezpod\\/tpenas\\/valid\\/20160229\\/014842672838___________00000_01.tif" } } } } json.TrackingRecord.MovementInformation.Movement.forEach(function(item) { //console.log(item); item.MovementDate = moment(item.MovementDate).format('ddd, Do of MMM YYYY'); item.MovementTime = moment(item.MovementTime).format('hh:mm a'); $("#movement tbody").append("<tr><td>" + item.MovementDate + "</td><td>" + item.MovementTime + "</td><td>" + item.Description + "</td></tr>"); }) 
      
     
 <script src="https://cdn.jsdelivr.net/momentjs/2.11.2/moment.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="movement"> <thead> <tr> <th>Date</th> <th>Time</th> <th>Status</th> </tr> </thead> <tbody> </tbody> </table> 

Not with plain forEach . 不能用普通forEach Possibilities: 可能性:

  • Assume it is in ascending order. 假设它是按升序排列的。 Loop indices from length - 1 to 0 . 循环索引的length - 1length - 10 Nondestructive. 无损。
  • Assume it is in ascending order. 假设它是按升序排列的。 Apply reverse , then forEach . reverse应用,然后forEach Destructive. 破坏性。
  • Assume it is unsorted. 假设它是未排序的。 Sort the array, then apply forEach . 对数组进行排序,然后申请forEach Destructive. 破坏性。
  • Assume it is unsorted. 假设它是未排序的。 Create an array of indices, sort the indices by the corresponding values in the array, apply forEach on indices and access array members. 创建索引数组,按数组中的相应值对索引排序,在索引上应用forEach并访问数组成员。 Nondestructive. 无损。
  • Assume it is unsorted. 假设它是未排序的。 Clone the array using slice , then apply sort and forEach . 使用slice克隆数组,然后应用sortforEach Nondestructive. 无损。

You could change 你可以改变

json.TrackingRecord.MovementInformation.Movement.forEach(function(item) {

to

json.TrackingRecord.MovementInformation.Movement.reverse().forEach(function(item) {
                                                 ^^^^^^^^^

 var json = { "TrackingRecord": { "Authorised": "Authorised(5.77.48.131)", "DeliveryAddress": { "CompanyName": "JAMES DERICK", "Address1": "6", "Address2": "LIBER HOUSE", "Address3": "OLYMPIAN", "Town": "YORK", "Postcode": "YO10 3UF", "ContactName": "JAMES DERICK", "ContactTelephone": "7507346318" }, "CollectionAddress": { "CompanyName": "AMBIENT LOUNGE LTD", "Address1": "UNIT 3 LONG HEDGE LANE INDUSTR", "Address2": "BOTTESFORD", "Address3": { }, "Town": "NOTTINGHAM", "Postcode": "NG13 0BF", "ContactName": "SARAH KIRBY", "ContactTelephone": "07879 442266074" }, "ConsignmentInformation": { "Pieces": "1", "Pallets": "0", "Weight": "10", "Service": "Priority 1", "DeliveryDate": "2016-02-29T00:00:00", "ItemsDelivered": "1", "ConsignmentRef": "2838", "SpecialInstructions": "JAMES DERICK 7507346318 {JAMES\
    
    
    
      14075@GMAIL.COM}\ 
     
       ", "AdditionalReferencesInformation": { "AdditionalReferences": { "Reference": "2838" } } }, "MovementInformation": { "Movement": [{ "MovementDate": "2016-02-25T00:00:00", "MovementTime": "0001-01-01T10:00:04", "Description": "Created By EZEEWEB", "DeliveryDepot": "Leeds", "Round": "019", "DeliveryDate": "2016-02-26T00:00:00", "PackagesReceived": "0", "PackagesDelivered": "0" }, { "MovementDate": "2016-02-26T00:00:00", "MovementTime": "0001-01-01T07:11:53", "Description": "Out to deliver", "DeliveryDepot": "Leeds", "Round": "019", "DeliveryDate": "2016-02-26T00:00:00", "PackagesReceived": "1", "PackagesDelivered": "0" }, { "MovementDate": "2016-02-26T00:00:00", "MovementTime": "0001-01-01T11:00:53", "Description": "Failed - Other reason", "DeliveryDepot": "Leeds", "Round": "019", "DeliveryDate": "2016-02-29T00:00:00", "PackagesReceived": "1", "PackagesDelivered": "0" }, { "MovementDate": "2016-02-27T00:00:00", "MovementTime": "0001-01-01T05:59:32", "Description": "Out to deliver", "DeliveryDepot": "Leeds", "Round": "019", "DeliveryDate": "2016-02-29T00:00:00", "PackagesReceived": "1", "PackagesDelivered": "0" }, { "MovementDate": "2016-02-29T00:00:00", "MovementTime": "0001-01-01T10:55:43", "Description": "Delivered", "DeliveryDepot": "Leeds", "Round": "019", "DeliveryDate": "2016-02-29T00:00:00", "PackagesReceived": "1", "PackagesDelivered": "1" }] }, "TimedInformation": { "TimedDelivery": { "Signature": "DERICK", "SignatureDate": "2016-02-29T00:00:00", "SignatureTime": "0001-01-01T10:55:00" } }, "ScanInformation": { "Scan": [{ "PieceID": "148426702251072001", "Description": "Auto Inbound Scan ()", "Depot": "Newark", "ScanDate": "2016-02-25T00:00:00", "ScanTime": "0001-01-01T17:12:01", "ScannedBy": "NWK CONVYR" }, { "PieceID": "148426702251072001", "Description": "Auto Inbound Scan ()", "Depot": "Leeds", "ScanDate": "2016-02-26T00:00:00", "ScanTime": "0001-01-01T02:22:08", "ScannedBy": "LDS CONVYR" }, { "PieceID": "148426702251072001", "Description": "Load C & D (019)", "Depot": "Leeds", "ScanDate": "2016-02-26T00:00:00", "ScanTime": "0001-01-01T03:37:45", "ScannedBy": "CJONES" }, { "PieceID": "148426702251072001", "Description": "Load C & D (019)", "Depot": "Leeds", "ScanDate": "2016-02-26T00:00:00", "ScanTime": "0001-01-01T23:43:22", "ScannedBy": "CJONES" }] }, "ImageInformation": { "PODImage": { "URL": "http:\\/\\/www.tpeweb.co.uk\\/ezpod\\/tpenas\\/valid\\/20160229\\/014842672838___________00000_01.tif" } } } } json.TrackingRecord.MovementInformation.Movement.reverse().forEach(function(item) { //console.log(item); item.MovementDate = moment(item.MovementDate).format('ddd, Do of MMM YYYY'); item.MovementTime = moment(item.MovementTime).format('hh:mm a'); $("#movement tbody").append("<tr><td>" + item.MovementDate + "</td><td>" + item.MovementTime + "</td><td>" + item.Description + "</td></tr>"); }) 
      
     
 <script src="https://cdn.jsdelivr.net/momentjs/2.11.2/moment.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="movement"> <thead> <tr> <th>Date</th> <th>Time</th> <th>Status</th> </tr> </thead> <tbody> </tbody> </table> 

Keep in mind that reverse actually modifies the array, it doesn't just return an array with the elements reversed. 请记住, reverse实际上会修改数组,它不仅会返回元素反转的数组。 However, this is probably what you want considering it's supposed to be printed in that order. 但是,考虑到它应该按该顺序打印,这可能是您想要的。

A non–destructive way to iterate backwards over an array (other than a plain for loop) is reduceRight : 向后遍历数组(非普通的for循环)的非破坏性方法是reduceRight

json.TrackingRecord.MovementInformation.Movement.reduceRight(function(acc, item) {
  // do stuff
}, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM