user2238083
user2238083

Reputation: 591

Get value from complex array

Hi I have an array (see below for the first part of the array) and I can get the name using the code (my code is in a loop to get all names from the array)

jsonFabric.values[i].name

which gives me "3002-023"

How do I get the labels name?

which would give me "Fabric".

I have tried many variations including

jsonFabric.values[i].labels['name']

but they do not get "Fabric"

{
    "totalRows": 151,
    "values": [
        {
            "width": 1338,
            "height": 2397,
            "isNew": true,
            "defaultScene": null,
            "displayUrl": "https://example.com/designs-324/3002%20023_small.png?1=1&width=500&Cache=Default&height=500&p.dc=1&mode=max&format=jpg&timestamp=636244299470877669",
            "renderUrl": "https://example.com/designs-324/3002%20023.tif?1=1&width=-1&Cache=Default&p.dc=1&mode=max&format=jpg&timestamp=636244299470877669",
            "designOptions": {
                "repeat": true,
                "width": 114,
                "height": 203,
                "gloss": 0,
                "contrast": 0,
                "dropX": 0,
                "dropY": 0,
                "placingPointX": 0.5,
                "placingPointY": 0.5,
                "flip": false,
                "rotation": 0
            },
            "id": 324,
            "name": "3002-023",
            "properties": [],
            "propertiesPerLabel": [],
            "labels": [
                {
                    "id": 1,
                    "parentId": 0,
                    "name": "Fabric",
                    "path": []
                }
            ],
            "description": null,
            "createDate": "2017-03-06T20:45:47.0877669",
            "lastSaveDate": "2017-03-09T13:49:38.5256163",
            "attachments": [],
            "storageName": "3002 023.tif",
            "storagePath": "designs-324/3002 023.tif",
            "relations": {
                "direct": []
            },
            "referenceId": "3002-023.tif"
        }, 

and so on.....

        {
            "width": 1354,
            "height": 1870,
            "isNew": true,

Upvotes: 0

Views: 273

Answers (2)

larz
larz

Reputation: 5766

labels is an array, so you need to either select the first element(if there is only one) or loop through to grab the name from each.

let obj = {
  "totalRows": 151,
  "values": [{
    "width": 1338,
    "height": 2397,
    "isNew": true,
    "defaultScene": null,
    "displayUrl": "https://example.com/designs-324/3002%20023_small.png?1=1&width=500&Cache=Default&height=500&p.dc=1&mode=max&format=jpg&timestamp=636244299470877669",
    "renderUrl": "https://example.com/designs-324/3002%20023.tif?1=1&width=-1&Cache=Default&p.dc=1&mode=max&format=jpg&timestamp=636244299470877669",
    "designOptions": {
      "repeat": true,
      "width": 114,
      "height": 203,
      "gloss": 0,
      "contrast": 0,
      "dropX": 0,
      "dropY": 0,
      "placingPointX": 0.5,
      "placingPointY": 0.5,
      "flip": false,
      "rotation": 0
    },
    "id": 324,
    "name": "3002-023",
    "properties": [],
    "propertiesPerLabel": [],
    "labels": [{
      "id": 1,
      "parentId": 0,
      "name": "Fabric",
      "path": []
    }],
    "description": null,
    "createDate": "2017-03-06T20:45:47.0877669",
    "lastSaveDate": "2017-03-09T13:49:38.5256163",
    "attachments": [],
    "storageName": "3002 023.tif",
    "storagePath": "designs-324/3002 023.tif",
    "relations": {
      "direct": []
    },
    "referenceId": "3002-023.tif"
  }]
}

console.log(obj.values[0].labels[0].name)

Upvotes: 1

Erazihel
Erazihel

Reputation: 7605

labels represents an array. You need to access the first object of this array to print its name:

jsonFabric.values[i].labels[0].name

Upvotes: 2

Related Questions