使用AngularJS从JSON检索项目

时间:2014-07-14 15:57:52

标签: json angularjs asp.net-mvc-4

我有一个ASP.Net MVC4应用程序。我已经获得了下面的JSON。我需要的是能够检索具有传递的CS值的特定项目的INDEX。 CS值将在ViewBag中传递,JSON也在下面的工厂函数中的ViewBag中传递。

CDlg.factory("RS", function () {
    var rs = @Html.Raw(ViewBag.jsonCR);
    return rs;
});

这是返回的rs:

[
   {
      "CS": "AK",
      "CLL": "LN",
      "CTC": [
         {
            "LT": null,
            "CTID": 227322,
            "Hors": 8,
            "Units": 8,
            "Price": 8,
            "CEE": 0
         }
      ]
   },
  {
      "CS": "TX",
      "CLL": "LN",
      "CTC": [
         {
            "LT": null,
            "CTID": 219848,
            "Hours": 8,
            "Units": 8,
            "Price": 6,
            "CEE": 0
         }
      ]
   },
   {
      "CS": "UT",
      "CLL": "LN",
      "CTC": [
         {
            "LT": null,
            "CTID": 219928,
            "Hours": 8,
            "Units": 8,
            "Price": 8,
            "CEE": 0
         }
      ]
   },
   {
      "CS": "VA",
      "CLL": "LN",
      "CTC": [
         {
            "LT": null,
            "CTID": 220041,
            "Hours": 8,
            "Units": 8,
            "Price": 0,
            "CEE": 0
         }
      ]
   },
   {
      "CS": "VT",
      "CLL": "LN",
      "CTC": [
         {
            "LT": null,
            "CTID": 220344,
            "Hours": 8,
            "Units": 8,
            "Price": 12,
            "CEE": 0
         }
      ]
   }
]

这是我需要填充的范围变量:

$scope.selectedCS = ?????

如果我传入" TX,我需要它来返回完整的对象:

  {
      "CS": "TX",
      "CLL": "LN",
      "CTC": [
         {
            "LT": null,
            "CTID": 219848,
            "Hours": 8,
            "Units": 8,
            "Price": 6,
            "CEE": 0
         }
      ]
   }

1 个答案:

答案 0 :(得分:0)

只需迭代并检查:

for (var i = 0; i < rs.length; i++) {
    if (rs[i].CS == passedInValue) {
        return rs[i];
    }
}