[英]Name from JSON response getting undefined when console.log
我下面是從API
獲取的JSON
。
{
"categories":[
{
"id":"20",
"parent_id":"3",
"name":"Khau Galli",
"description":null,
"sort_order":"58",
"is_visible":"1",
"created_at":"2018-05-20 07:47:36",
"updated_at":"2018-06-12 13:50:38",
"icon":null
},
{
"id":"22",
"parent_id":"3",
"name":"Apparel Wear",
"description":null,
"sort_order":"59",
"is_visible":"1",
"created_at":"2018-05-20 07:50:34",
"updated_at":"2018-06-12 13:50:38",
"icon":null
},
{
"id":"204",
"parent_id":"3",
"name":"Footwear",
"description":null,
"sort_order":"227",
"is_visible":"1",
"created_at":"2018-05-24 10:26:27",
"updated_at":"2018-06-12 13:50:38",
"icon":null
},
{
"id":"205",
"parent_id":"3",
"name":"Fruits & Vegetable Vendor",
"description":null,
"sort_order":"228",
"is_visible":"1",
"created_at":"2018-05-24 10:28:26",
"updated_at":"2018-06-12 13:50:38",
"icon":null
},
{
"id":"327",
"parent_id":"3",
"name":"Paan Shop",
"description":null,
"sort_order":"340",
"is_visible":"1",
"created_at":"2018-06-04 10:01:02",
"updated_at":"2018-06-12 13:50:38",
"icon":null
}
]
}
我想從離子搜索欄的響應中取名字。 但是,當我使用console.log name
它的name
不確定。 我寫了下面的代碼
this.authService.postData("id=" + id, "category").then((result) => {
this.resposeData = result;
this.categories = this.resposeData.categories;
this.categoriesName = this.categories.name;
console.log(this.categoriesNamem);
//this.listings = this.resposeData.data.list.data;
//console.log(this.categories);
// localStorage.setItem('userData', JSON.stringify(this.resposeData) )
// this.navCtrl.push(Login);
this.loading.dismiss();
})
即使我在JSON.stringify
上嘗試了this.categories
。 但仍給未定義。 請幫忙。 我想在離子搜索的以下結構中獲得所有這些名稱。
initializeItems() {
this.items = [
'Amsterdam',
'Bogota',
...
];
}
this.item
來自name
類別是一個數組。 嘗試this.categories [0] .name。 這將為您提供第一類的名稱
您只需要遍歷類別並將每個名稱字段存儲到數組中即可。
this.authService.postData("id=" + id, "category").then((result) => {
this.resposeData = result;
this.categories = this.resposeData.categories;
// Iterate through categories and store each name into this.names
this.names = this.categories.map(cat => cat.name);
//this.listings = this.resposeData.data.list.data;
//console.log(this.categories);
// localStorage.setItem('userData', JSON.stringify(this.resposeData) )
// this.navCtrl.push(Login);
this.loading.dismiss();
});
類別是對象的數組,所以我認為您必須嘗試這樣的事情
this.categoriesName = this.categories[0].name;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.