从复杂的数组中获取价值

时间:2017-05-01 21:20:01

标签: javascript jquery arrays

您好我有一个数组(请参阅下面的数组的第一部分),我可以使用代码获取名称(我的代码在循环中以获取数组中的所有名称)

jsonFabric.values[i].name

给了我" 3002-023"

如何获取标签名称?

这会给我" Fabric"。

我尝试了很多变种,包括

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

但他们没有得到" 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"
        }, 

依旧.....

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

2 个答案:

答案 0 :(得分:2)

labels代表array。您需要访问此数组的第一个对象以打印其名称:

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

答案 1 :(得分:1)

labels是一个数组,所以你需要选择第一个元素(如果只有一个)或循环来从每个元素中获取name



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)