gpt4 book ai didi

javascript - 带有 JSON 对象数组过滤器的 Angular JS Controller

转载 作者:行者123 更新时间:2023-12-01 02:58:22 26 4
gpt4 key购买 nike

我有以下类型的 JSON,我想查找 JSON 中 Status : 1 的对象数量,我尝试了以下方法,但它不起作用。

我知道 ng-filter 只需要应用于数组,但我没有找到在下面的 JSON 数据中执行此操作的方法,有人可以帮助我

我收到了预期的错误,但在我的情况下如何解决此问题

[filter:notarray] 预期数组但收到:

Plunker Demo

在下面的 JSON 数据中,如您所见,我有带有数组的数组,我应该得到状态值为 1

的计数

预期输出

第一个数组的计数:1第二个数组的计数:3第三个数组的计数:1

JSON 数据

    [
[{
"id": 252323,
"orderId": 223505,
"status": 1,
"executedOn": "13/Sep/17",
"executedBy": "Person A",
"executedByDisplay": "Person A",
"cycleId": 3994,
"cycleName": "Cycle A",
"versionId": 21291,
"versionName": "Version A",
"issueKey": "Key-12561"
}],
[{
"id": 253077,
"orderId": 224047,
"status": 1,
"executedOn": "26/Sep/17",
"executedBy": "Person B",
"executedByDisplay": "Person B",
"cycleId": 4012,
"cycleName": "Cycle B",
"versionId": 22912,
"versionName": "Version B",
"issueKey": "Key-12580"
}, {
"id": 253076,
"orderId": 224046,
"status": 2,
"executedOn": "20/Sep/17",
"executedBy": "Person B",
"executedByDisplay": "Person B",
"cycleId": 4012,
"cycleName": "Cycle B",
"versionId": 22912,
"versionName": "Version B",
"issueKey": "Key-12578"
}, {
"id": 253075,
"orderId": 224045,
"status": 1,
"executedOn": "20/Sep/17",
"executedBy": "Person B",
"executedByDisplay": "Person B",
"cycleId": 4012,
"cycleName": "Cycle B",
"versionId": 22912,
"versionName": "Version B",
"issueKey": "Key-12582"
}, {
"id": 253074,
"orderId": 224044,
"status": 1,
"executedOn": "20/Sep/17",
"executedBy": "Person B",
"executedByDisplay": "Person B",
"cycleId": 4012,
"cycleName": "Cycle B",
"versionId": 22912,
"versionName": "Version B",
"issueKey": "Key-12584"
}],
[{
"id": 255168,
"orderId": 226138,
"status": 1,
"executedOn": "26/Sep/17",
"executedBy": "Person A",
"executedByDisplay": "Person A",
"cycleId": 4022,
"cycleName": "Cycle C",
"versionId": 21291,
"versionName": "Version A",
"issueKey": "Key-12617"
}]
]

Angular JS 代码

var app = angular.module('studentApp', []);

app.controller('StudentCntrl', function($scope, $http, $filter) {
$http.get('data.json').then(function(response) {

$scope.value = response.data;

$scope.value.forEach(function(items) {
$scope.eachValue = items;
items.forEach(function(insideItems) {
passvalue = $filter('filter')(insideItems, function(inputs) {
if (inputs.status == '1')
console.log(inputs.length);
return inputs;
});
});

});
});
});

最佳答案

您可以简单地使用 java script array filter类似的东西

 arr.filter( function(a){return a.status==1}).length

或者使用 Angular

 $filter('filter')(v, function(vc) {return vc.status ==1}).length

Here is working demo

关于javascript - 带有 JSON 对象数组过滤器的 Angular JS Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46598074/

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