2017-07-08 52 views
0

我在下面給出的是我的json對象如何獲得citydetails,其中city_id = 50?如何根據角度使用類別得到特定的細節2

{ 
"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" 

    } 
] 
} 
+0

您的JSON對象是錯誤的 – NuttLoose

+0

這裏是完整的陣列?你試圖過濾什麼? – Aravind

+0

@ Aravind,@ NuttLoose:我糾正了我的json,現在它是有效的。我想獲取city_id = 50的城市詳細信息。請幫助我 –

回答

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對象將只包含過濾城市對象作爲數組

+0

@ NuttLoose - Kindly我將在我的.ts頁面添加過濾器。目前我從構造函數中獲得的json對象。如何添加文件管理器以及我將添加的位置。下面是我的代碼裏面的構造函數:city_id:any; cityType:any [] = []; 構造函數(公共navCtrl:NavController,私人appMsg:AppCommonService,公共navParams:導航){ this.city_id = navParams.get('id'); this.cityType = this.appMsg.retrieveCityDetails(); } –

+0

我在你的構造函數中沒有看到任何json對象,你可以通過將你得到的json賦值給任何變量來應用它。只需添加variable.filter()即可。 – NuttLoose

+0

@ NuttLoose - Gud Morg,好的,我會檢查它,我會告訴你。我從commonservice中檢索對象數組:this.cityType = this.appMsg.retrieveCityDetails();.我有id和對象數組。我想過濾,我需要獲得特定的城市細節 –

相關問題