0

I am trying to fetch a object in ionic2 which has key value and i want to read the key and the brand name from it within ionic2.

so my question :

how i can read the key from the below object ?

{
    "status": "success",
    "products": {
        "Key1": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
        "Key2": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
        "Key3": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
    }
}

please feel free to help me.

Mahmoud Ismail
  • 1,617
  • 4
  • 27
  • 51

1 Answers1

0

If you're variable which contains this is called data.

data.products will return the list of products. (or 'Key's in your case.)

Then calling data.products.Key1 will give you the object of Key1.

data.products.Key1.entity_id will return '448'.

So logically you can get the brandname by calling data.prodcuts.Key1.brand

Edit misread:

Since your Key1 is a list for some reason.

data.products.Key1[0].brand

Ivar Reukers
  • 7,560
  • 9
  • 56
  • 99
  • thank you for your response but the`key1` and all other `key` i want to get dynamically any suggestion ? – Mahmoud Ismail Nov 07 '16 at 13:54
  • Taken from https://github.com/paularmstrong/normalizr/issues/15 it says you can use `for(let key of data.products)` but I haven't tested myself – Ivar Reukers Nov 07 '16 at 14:00