我有Array
已在下面給出。在這個數組中,一些對象包含Key catagoryName。對於每個單獨的類別名稱我需要得到計數,其中ReadDate爲空。獲取數組基於samecategoryname使用anjularjs
質詢強調:
- 推入陣列基於每個類別(類別名稱可能是 'N' 數量)。
獲取ReadDate爲空的每個分類計數。
.controller('testController',function($scope,$rootScope){ $rootScope.messages = [ { "IntMessageUserId": 9, "ReadDate": null, "MessageSubject": "HOLY CRAP ", "MessageText": "blah blah blah", "Priority": 1, "PriorityDescription": "High", "StartDate": "2017-01-19T00:00:00", "EndDate": "2017-07-01T00:00:00", "IntMessageCategoryId": 36, "CategoryName": "a New Category By Dave", "CategoryDescription": "Only a couple", "ActiveSw": true }, { "IntMessageUserId": 110, "ReadDate": "null", "MessageSubject": "Test_chm2", "MessageText": "Test_chm2", "Priority": 2, "PriorityDescription": "Urgent", "StartDate": "2017-01-04T00:00:00", "EndDate": "2017-06-02T00:00:00", "IntMessageCategoryId": 39, "CategoryName": "Alerts", "CategoryDescription": "Alerts", "ActiveSw": true }, { "IntMessageUserId": 117, "ReadDate": "2017-05-05T06:35:00", "MessageSubject": "Test_chm2", "MessageText": "Test_chm2", "Priority": 1, "PriorityDescription": "High", "StartDate": "2017-01-04T00:00:00", "EndDate": "2017-06-02T00:00:00", "IntMessageCategoryId": 39, "CategoryName": "Alerts", "CategoryDescription": "Alerts", "ActiveSw": true }, { "IntMessageUserId": 113, "ReadDate": "2017-05-03T08:48:00", "MessageSubject": "Testing PSC", "MessageText": "Psc note that displays until May 26", "Priority": 1, "PriorityDescription": "High", "StartDate": "2017-04-24T09:31:00", "EndDate": "2017-05-26T09:31:00", "IntMessageCategoryId": 59, "CategoryName": "Alerts PSC", "CategoryDescription": "Alerts PSC", "ActiveSw": true }, { "IntMessageUserId": 130, "ReadDate": null, "MessageSubject": "17/18 Parking Pass sale dates", "MessageText": "Parking Passes will be available for purchase for the 17/18 school year for all current Juniors starting on June 1st. Sophomores may purchase passes starting on August 1st. Sales are in person only at the school district offices. You must fill out our parking permit form, Show your current drivers license, and proof of insurance. Passes will be sold for $50.00 for the school year. \r\n\r\n", "Priority": 0, "PriorityDescription": "Normal", "StartDate": "2017-05-05T10:40:00", "EndDate": "2017-06-01T10:39:00", "IntMessageCategoryId": 67, "CategoryName": "High School Students", "CategoryDescription": "High School students", "ActiveSw": true }, { "IntMessageUserId": 111, "ReadDate": "2017-05-03T08:50:00", "MessageSubject": "Summer Sports Camps", "MessageText": "Our summer sports camps flyers will be sent home in the next week or 2. Please check them out and see if you would like to enroll your child in a sports camp program. We encourage them to try something new, or improve their skills in an existing sport. ", "Priority": 0, "PriorityDescription": "Normal", "StartDate": "2017-03-21T00:00:00", "EndDate": "2017-05-15T00:00:00", "IntMessageCategoryId": 69, "CategoryName": "Athletic Department Notifications ", "CategoryDescription": "Athletic Department.", "ActiveSw": false }, { "IntMessageUserId": 131, "ReadDate": null, "MessageSubject": "End of Year Lunch balances", "MessageText": "As we come to the end of our school year, a reminder that unused lunch funds will roll over to next school year and be available next year in the lunch line. If you have a student who is graduating and has unused funds, please consider donating those funds to a student in need.. You can contact the Food Service department at 513-555-5555 and we will be happy to coordinate this. Refunds of unused lunch funds can be issued for any student who's balance is greater than $5.00 Please fill out a refund form at the school office and they will be processed by our accounting department. Refund checks will be mailed within 30 days of receiving your forms in the office. Have a great summer. ", "Priority": 0, "PriorityDescription": "Normal", "StartDate": "2017-05-05T10:45:00", "EndDate": "2017-06-09T10:45:00", "IntMessageCategoryId": 80, "CategoryName": "Nutrition Services", "CategoryDescription": "Nutrition Services", "ActiveSw": true } ];
我想,直到什麼。
angular.forEach($rootScope.messages,function(value, key){
$scope.checkUnreadMessages(value.CategoryName);
})
var mainArray = [];
$scope.call = function(){
angular.forEach(arr, function(value, key){
var subarr = [];
subarr[value] = [];
angular.forEach($rootScope.messages, function(v, k){
if(value == v.CategoryName){
subarr[value].push(v.ReadDate);
}
})
mainArray.push(subarr);
})
}
見這個例子http://jsfiddle.net/W2Z86/256/ – NNR
我更新回答,所以它也做了第二部分。我不認爲這是最好的解決方案,它不是,一個應該能夠使用更少的空間複雜度等。但它是一個快速修復無論如何 – Alburkerk