gpt4 book ai didi

javascript - 如何在angularjs中获取响应数组值的asc顺序

转载 作者:行者123 更新时间:2023-11-28 14:52:13 27 4
gpt4 key购买 nike

大家好,我正在使用 angularjs,我有一组数据并将其放入一个作用域变量中,现在我需要的是我需要根据日期“MONTH”我的日期格式对获取的数组值进行排序DispenseMonth": "9/1/2016"(MM/DD/YY) 这是我的格式,我附加了 fiddle ,现在我需要的是我根据 DispenseMonth -orderby Month 获取数组顺序帮助如何做到这一点

https://jsfiddle.net/kvbgrehr/1/

最佳答案

使用 getMonth() 对日期对象进行排序。

var app = angular.module("Filter", []);
app.controller('StateShowCtrl', ['$scope', '$http',
function ($scope, $http) {

var data=[
{
"PatientState": "AK",
"DispenseMonth": "7/1/2016"

},
{
"PatientState": "AK",
"DispenseMonth": "8/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "9/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "10/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "11/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "2/2/2017"
},
{
"PatientState": "AK",
"DispenseMonth": "3/5/2017"
}
]

$scope.StateInformations =data.sort(function(a, b) {
return new Date(a.DispenseMonth).getMonth() - new Date(b.DispenseMonth).getMonth();
});
console.log( $scope.StateInformations);
}
]);
<!DOCTYPE html>
<html ng-app="Filter">
<head>
<!-- basic scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>


</head>
<body class="no-skin" ng-controller="StateShowCtrl">
</body>

关于javascript - 如何在angularjs中获取响应数组值的asc顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44107417/

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