如何使用角度2基于类别获取特定细节

时间:2017-07-08 12:55:46

标签: json angular ionic-framework

我在下面给出的是我的json对象如何获得city_id = 50的citydetails?

{
"status": 0,
"message": "success",
"citydetails": [
  {
        "city_id": "50",
        "city_name": "Pune"

    },
    {
        "city_id": "53",
        "city_name": "Mumbai"

    },
    {
        "city_id": "50",
        "city_name": "Pune"
    },
    {
        "city_id": "52",
        "city_name": "Banglore"

    },
    {
        "city_id": "50",
        "city_name": "Pune"

    }
]
}

1 个答案:

答案 0 :(得分:0)

let json = {
 "status":0,
 "message":"success",
 "citydetails":[
  {
     "city_id":"50",
     "city_name":"Pune"
  },
  {
     "city_id":"53",
     "city_name":"Mumbai"
  },
  {
     "city_id":"50",
     "city_name":"Pune"
  },
  {
     "city_id":"52",
     "city_name":"Banglore"

  },
  {
     "city_id":"50",
     "city_name":"Pune"
  }]
};

在上面的JSON中假设,下面的代码将过滤城市对象。

let filteredCities = json.citydetails.filter(data => {
  return data.city_id === "50";
});

filteredCities对象将仅包含已过滤的城市对象作为数组