gpt4 book ai didi

javascript - Angularjs ng-repeat 有一些条件 :

转载 作者:行者123 更新时间:2023-11-29 15:16:06 25 4
gpt4 key购买 nike

这是我的 json 响应,我将其存储在

$scope.times = response.data;

我的 $scope.times json 对象:

[
{
"id": 1,
"status": true,
"time": "2018-03-05T10:24:15.000Z",
"complaintId": 1
},
{
"id": 2,
"status": true,
"time": null,
"complaintId": 1
},
{
"id": 3,
"status": true,
"time": "2018-03-05T10:53:14.000Z",
"complaintId": 2
},
{
"id": 6,
"status": false,
"time": "2018-03-05T11:58:45.000Z",
"complaintId": 1
},
{
"id": 7,
"status": true,
"time": "2018-03-05T12:11:53.000Z",
"complaintId": 1
},
{
"id": 8,
"status": false,
"time": "2018-03-05T13:23:13.000Z",
"complaintId": 2
},
{
"id": 9,
"status": true,
"time": "2018-03-05T08:17:18.000Z",
"complaintId": 3
},
{
"id": 10,
"status": true,
"time": "2018-03-05T12:32:08.000Z",
"complaintId": 2
}
]

我使用 ng-repeat= 'time in times' 在 html 中显示它,但我需要获取时间的 json 对象,其中 complaintId 位于最后,例如有很多 complaintId: 1 在对象中。

我只需要获取最后有 complaintId 的。

我在时间的预期输出:

[
{
"id": 7,
"status": true,
"time": "2018-03-05T12:11:53.000Z",
"complaintId": 1
},
{
"id": 9,
"status": true,
"time": "2018-03-05T08:17:18.000Z",
"complaintId": 3
},
{
"id": 10,
"status": true,
"time": "2018-03-05T12:32:08.000Z",
"complaintId": 2
}
]

我的 ng-repeat 循环代码:

table
tr
th S.no
th Time
tr(ng-repeat='time in times')
td {{$index + 1 }}
td {{time.status}}

最佳答案

(不要判断,今天是星期一)

这是我能找到的最佳解决方案。你可以明显地改进它(让它更短)通过使用一些 ES6 魔法 .reduce/.filter/.forEach

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<div ng-repeat="time in times">
<pre>{{time | json}}</pre>
</div>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.times = [{"id":1,"status":true,"time":"2018-03-05T10:24:15.000Z","complaintId":1},{"id":2,"status":true,"time":null,"complaintId":1},{"id":3,"status":true,"time":"2018-03-05T10:53:14.000Z","complaintId":2},{"id":6,"status":false,"time":"2018-03-05T11:58:45.000Z","complaintId":1},{"id":7,"status":true,"time":"2018-03-05T12:11:53.000Z","complaintId":1},{"id":8,"status":false,"time":"2018-03-05T13:23:13.000Z","complaintId":2},{"id":9,"status":true,"time":"2018-03-05T08:17:18.000Z","complaintId":3},{"id":10,"status":true,"time":"2018-03-05T12:32:08.000Z","complaintId":2}];

var a = $scope.times;

var b = []; // collect complaintId
for(var i=0;i<a.length;i++){
if(!b.includes(a[i].complaintId)){
b.push(a[i].complaintId);
}
}

var c = []; // collect duplicates per complaintId
for(var i=0;i<b.length;i++){
var temp = []
for(var j=0;j<a.length;j++){
if(b[i] == a[j].complaintId){
temp.push(a[j])
}
}
c.push(temp)
}

// now it's safe to `reduce` the inner arrays (per complaintId)

var d = []; // .forEach .reduce
for(var i=0; i<c.length;i++){
var temp = c[i][0].time;
var temp2 = c[i][0];
for(var j=1; j<c[i].length;j++){
if(c[i][j].time){
if(new Date(c[i][j].time).valueOf() > new Date(temp).valueOf()){
temp = c[i][j].time;
temp2 = c[i][j];
}
}
}
d.push(temp2)
}

$scope.times = d;

});

</script>

</body>
</html>

关于javascript - Angularjs ng-repeat 有一些条件 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49111513/

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