簡體   English   中英

如何獲取帶有角的對象內屬性的長度

[英]How to get the length of an attribute within an object with angular

我有一個稱為程序的json對象。 在這個對象中,我有一個鍵/值數組。 我需要獲取多少個對象具有“狀態”的鍵/值的計數:“審閱”。 因此,這里有4個對象,其中兩個具有“狀態”:“審閱”。 我需要像這樣返回此計數(2)

{
    "id": 3,
    "organizationName": "Watertown School",
      "programs": [
          {
              "id": 72,
              "programId": 456456,
              "programName": "name 1",
              "programType": "Phd",
              "applicationType": "Domestic",
              "concentration": "Medicine",
              "formId": 0,
              "isReviewed": true,
              "status": "Review"
          },
          {
              "id": 73,
              "programId": 4564561,
              "programName": "name 2",
              "programType": "Phd",
              "applicationType": "Domestic",
              "concentration": "Medicine",
              "formId": 0,
              "isReviewed": true,
              "status": "Draft"
          },
          {
              "id": 74,
              "programId": 10343431,
              "programName": "name 3",
              "programType": "Phd",
              "applicationType": "Domestic",
              "concentration": "Medicine",
              "formId": 0,
              "isReviewed": true,
              "status": "Review"
          },
          {
              "id": 75,
              "programId": 10031,
              "programName": "namw 4",
              "programType": "Phd",
              "applicationType": "Domestic",
              "concentration": "Medicine",
              "formId": 0,
              "isReviewed": true,
              "status": "Draft"
          },
      ]
}

取決於您需要什么? 如果在dom中,您可以執行以下操作:

{{ (myObject.programs | filter:{ status: 'Review' }).length }}

這是顯示它的jsFiddle: http : //jsfiddle.net/2jx5oL78/

最好的選擇可能是Array.prototype.filter() ,它不是特定於Angular的。 例如:

// Assuming you had your object as obj
var obj = {}; //Stuff goes here
obj.programs = obj.programs.filter(function (value) {
  if (value.hasOwnProperty('status') && value.status === "Review") {
    return true;
  }
  return false;
});

從那里,您可以只調用obj.programs.length

可以將過濾后的數組分配給任何變量,就像注釋一樣。 因此,如果您只想計數,那可能是有利的。

如果您想進行基於DOM的過濾器,則可以使用Mathew Berg建議的Angular過濾器,也可以使用ngIfngShow重復項中的內容,或者將上述過濾器直接應用於重復項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM