javascript foreach on array / json data 但按最新日期排序
javascript forEach on array / json data but sortby latest date?
是否可以
修改以下内容,以便在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'u003Cbr'u003E14075@GMAIL.COM}'u003Cbr'u003E",
"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>
不是普通的forEach
.可能性:
- 假设它是按升序排列的。从
length - 1
到0
的循环索引。非破坏性的。 - 假设它是按升序排列的。应用
reverse
,然后forEach
。破坏的。 - 假设它是未排序的。对数组进行排序,然后应用
forEach
。破坏的。 - 假设它是未排序的。创建一个索引数组,按数组中的相应值对索引进行排序,对索引应用
forEach
并访问数组成员。非破坏性的。 - 假设它是未排序的。使用
slice
克隆阵列,然后应用sort
并forEach
。非破坏性的。
向
后迭代数组(除了普通的 for 循环)的一种非破坏性方法是 reduceRight:
json.TrackingRecord.MovementInformation.Movement.reduceRight(function(acc, item) {
// do stuff
}, null);
你可以
改变
json.TrackingRecord.MovementInformation.Movement.forEach(function(item) {
自
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'u003Cbr'u003E14075@GMAIL.COM}'u003Cbr'u003E",
"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>
请记住,reverse
实际修改数组,它不只是返回一个元素颠倒的数组。但是,考虑到它应该按该顺序打印,这可能是您想要的。
相关文章:
- 比较从函数和生成的日期对象
- Json数据包含日期和时间格式
- Javascript日期格式类似于ISO,但本地
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 如何在JavaScript中延长当前日期
- jQuery-2.1.1.min.js或最新版本jQuery-2.13.min.js不会't支持'@
- 两位数的月份日期验证
- 更改angularjs中的日期-时间格式
- addData()从最新图表中删除.js 2.1.3-怎么了
- 将日期时间作为 JSON 发送将无法在我的视图中正确显示
- 将日期时间从json转换为可读格式
- 从日期数组中返回最新日期
- 从客户端的对象数组中获取最新日期的优雅方法是什么
- javascript foreach on array / json data 但按最新日期排序
- 你如何获得当前日期's的开始时间和最新时间(以毫秒为单位)
- MongoDB在集合中查找最新日期
- 读取appache清单值以获取最新更新日期
- 使用jquery,根据日期按时间顺序排序JSON,最新的在前
- 如何从节点组中查找最新的元素,其中元素的属性表示日期和时间.在xml中
- Javascript -最新日期和最高优先级