2017-05-07 44 views
1

我有Array已在下面給出。在這個數組中,一些對象包含Key catagoryName。對於每個單獨的類別名稱我需要得到計數,其中ReadDate爲空。獲取數組基於samecategoryname使用anjularjs

質詢強調:

  1. 推入陣列基於每個類別(類別名稱可能是 'N' 數量)。
  2. 獲取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); 

    }) 

} 
+0

見這個例子http://jsfiddle.net/W2Z86/256/ – NNR

+0

我更新回答,所以它也做了第二部分。我不認爲這是最好的解決方案,它不是,一個應該能夠使用更少的空間複雜度等。但它是一個快速修復無論如何 – Alburkerk

回答

0

我一般不喜歡使用額外的庫一個簡單的工作可以在純JavaScript可以輕鬆完成。

https://repl.it/Hk45/2

這裏是一個JavaScript編譯器,你可以看到一個解決方案的鏈接。所有的工作都是由非常簡單的函數來完成

注意:

function fillOrInitiate(category, element){ 

    if(categories[category]){ 
    categories[category].push(element); 
    } else { 
    categories[category] = []; 
    categories[category].push(element); 
    } 
} 

然後,只需通過數據迭代。

編輯:照顧你的數據,有時ReadDate是空的,有時它是「空」。不過,我在函數CountReadDateNull中處理了這個問題。

你不應該能夠直接通過打印最後一個選項卡可視化的數據,但它是在這裏你可以看到在第二的console.log

+0

檢查我的更新問題 – Khalid

0

爲什麼不使用Underscore.js

需要根據類別名稱提取陣列。

_.where($rootScope.messages, {CategoryName: "searching category name"}); 

獲取每個類別的計數,其中ReadDate爲空。

_.where($rootScope.messages, {ReadDate: null}).length; 
+0

檢查我的更新問題。我想你可能會以錯誤的方式回答我的問題。在上面{CategoryName:「搜索類別名稱」}。數組類可以是n數。需要分類排序每個類別,並在一個新陣列puch。 – Khalid

+0

where是一個你可以通過angular.copy簡單複製的數組的響應。 (如果你想改變它,不想改變主要數據) – mastermind

+0

還有什麼我錯過了嗎? – mastermind

0

1)通過消息的數組迭代和檢查同一類別名稱,然後將其推入新數組中,然後您需要使用類別名稱作爲鍵將其分組。

var groupCategory = {}; 
    angular.forEach($rootScope.messages, function (oldVal, newVal) { 
     if (oldVal.CategoryName === newVal.CategoryName) { 
      if (angular.isDefined(groupCategory[newVal.CategoryName])) { 
       groupCategory[newVal.CategoryName].push(newVal); 
      } else { 
       groupCategory[newVal.CategoryName] = [newVal]; 
      } 
     } 
    }); 

2)然後,您可以通過過濾器/計數類組確認,如果ReadDate是空

var cnt = 0; 
    angular.forEach(sameCategoryObj, function (val) { 
     if(val.ReadDate === null) { 
      cnt++; 
     } 
    });