gpt4 book ai didi

javascript - 如果数组具有匹配的字符串,则返回 Angular 自定义过滤器

转载 作者:行者123 更新时间:2023-11-30 15:33:23 24 4
gpt4 key购买 nike

为了解决这个问题,我绞尽脑汁苦思冥想了一段时间。我觉得我快到了,但无法正确完成最后一 block 工作。这就是我想要做的。我正在创建一个自定义过滤器,它将循环遍历我的 json 数组以查看是否有“status”键,如果该“status”键包含“SUCCESS”字段,它将返回与该数组匹配的所有对象。如果“状态”键不存在,则不显示数组对象。我可以看到我的循环看起来是正确的,因为它正在确定哪个对象数组包含关键字,但是当我尝试打印出我的结果时,我什么也没看到。请帮忙!

这是演示。
https://jsfiddle.net/missggnyc/6r9oophu/20/

HTML

<div ng-app="mySessions">   
<div ng-controller="sessionController">
<table>
<tr>
<td>Session ID</td>
<td>Session Name</td>

</tr>
<tr ng-repeat="cs in cSessions | successSessionFilter">
<td>{{cs.id}}</td>
<td>{{cs.session_name}}</td>
</tr>
</table>
</div>

JS

var app = angular.module('mySessions', []);
app.controller("sessionController", ['$scope', function($scope){
$scope.cSessions = [
{"id":2,"session_name":"BD20","session_description":"","status":["INPROGRESS"]},
{"id":3,"session_name":"CS03","session_description":"","status":["INPROGRESS", "SUCCESS"]},
{"id":4,"session_name":"MIS99","session_description":"","status":["FAIL","NOTINPROGRESS"]},
{"id":5,"session_name":"XGY7","session_description":"","status":["SUCCESS", "NOTINPROGRESS"]},
{"id":6,"session_name":"DCCC2","session_description":""},
{"id":7,"session_name":"IUOS33","session_description":""}
];
}]);
app.filter('successSessionFilter', function(){
return function(cObj){
angular.forEach(cObj, function(value, key){
angular.forEach(value, function(v, k){
//console.log('k is ' + k + ' and v is '+ v);
if(k === 'status'){
if(v.indexOf("SUCCESS") > -1){ //if it exists
//console.log("SUCCESS exists "+ v.indexOf("SUCCESS"));
//console.log(v);
return true;
}
else { //it the status field isn't there, then don't show the object
return false;
}
}
else
return false;
})
})
// console.log(cObj);
}
});

最佳答案

开始了

app.filter('successSessionFilter', function(){
return function(items){
var filtered = items.filter(function(item){
if(item['status'] && item['status'].indexOf('SUCCESS') != -1){
return item;
}
})
return filtered;
}
});

演示 https://jsfiddle.net/ogycjbkL/

关于javascript - 如果数组具有匹配的字符串,则返回 Angular 自定义过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41945355/

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