简体   繁体   English

当console.log时,来自JSON响应的名称变得不确定

[英]Name from JSON response getting undefined when console.log

I have below JSON getting from API . 我下面是从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
      }
   ]
}

I want to take name from the response for ionic search bar. 我想从离子搜索栏的响应中取名字。 But when I console.log name it's giving undefined. 但是,当我使用console.log name它的name不确定。 I wrote below code 我写了下面的代码

   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();

        })

Even I tried JSON.stringify on this.categories . 即使我在JSON.stringify上尝试了this.categories But still giving undefined. 但仍给未定义。 Please help. 请帮忙。 I want to get this all name in below structure for ionic search. 我想在离子搜索的以下结构中获得所有这些名称。

initializeItems() {
    this.items = [
      'Amsterdam',
      'Bogota',
      ...
    ];
  }

Where this.item coming from name this.item来自name

Categories is an array. 类别是一个数组。 Try this.categories[0].name. 尝试this.categories [0] .name。 This will give you the name of the first category 这将为您提供第一类的名称

You simply need to iterate over the categories and store each name field into an array. 您只需要遍历类别并将每个名称字段存储到数组中即可。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM