首页 > 解决方案 > Angular - 从对象数组中获取数据

问题描述

早上好,谁能给我建议?我很无知。我已经花了几个小时,我不知道如何解决这个问题。

数据是虚构的,原始 JSON 要复杂得多。

JSON

   {
    "main": [
        [
            {
                "type": "dasdasdasd",
                "id": 5,
                "content": {
                    "title": "adadadsad",
                    "items": [
                        {
                            "date": "2012-02-02T11:23:00Z",
                            "id": 12,
                            "name": "test",
                            "isEnabled": false,
                            "isHighlited": false,
                            "images": {
                                "lists": {
                                    "small": [
                                        {
                                            "id": 18,
                                            "position": 0,
                                            "titleImage": true,
                                            "url": "",
                                            "thumbnailReady": true
                                        }
                                    ],
                                    "original": [
                                        {
                                            "id": 19,
                                            "position": 0,
                                            "titleImage": true,
                                            "url": "",
                                            "thumbnailReady": true
                                        }
                                    ],
                                    "large": [
                                        {
                                            "id": 22,
                                            "position": 0,
                                            "titleImage": true,
                                            "url": "",
                                            "thumbnailReady": true
                                           
                                        }
                                    ],
                                    "medium": [
                                        {
                                            "id": 23,
                                            "position": 0,
                                            "titleImage": true,
                                            "url": "",
                                            "thumbnailReady": true
                                            
                                        }
                                    ]
                                }
                            },
                            "enum": "LINIE",
                            "url": "https://test.com"
                        }
                    ]
                }
            }
        ]
    ],
    "second": [
    ]
}

如何从 main -> content -> items -> images -> url 获取?更准确地说,每个图像和来自该 url。

非常感谢您的帮助。

标签: arraysjsonangulartypescriptobject

解决方案


看起来您的主数组有一个数组作为第一个元素。如果这是正确的,您将获取 main 的第一个元素,然后对其进行循环。

let jsonObj = {
    "main": [
        [
            {
                "type": "dasdasdasd",
                "id": 5,
                "content": {
                    "title": "adadadsad",
                    "items": [
                        {
                            "date": "2012-02-02T11:23:00Z",
                            "id": 12,
                            "name": "test",
                            "isEnabled": false,
                            "isHighlited": false,
                            "images": {
                                "lists": {
                                    "small": [
                                        {
                                            "id": 18,
                                            "position": 0,
                                            "titleImage": true,
                                            "url": "",
                                            "thumbnailReady": true
                                        }
                                    ],
                                    "original": [
                                        {
                                            "id": 19,
                                            "position": 0,
                                            "titleImage": true,
                                            "url": "",
                                            "thumbnailReady": true
                                        }
                                    ],
                                    "large": [
                                        {
                                            "id": 22,
                                            "position": 0,
                                            "titleImage": true,
                                            "url": "",
                                            "thumbnailReady": true
                                           
                                        }
                                    ],
                                    "medium": [
                                        {
                                            "id": 23,
                                            "position": 0,
                                            "titleImage": true,
                                            "url": "",
                                            "thumbnailReady": true
                                            
                                        }
                                    ]
                                }
                            },
                            "enum": "LINIE",
                            "url": "https://test.com"
                        }
                    ]
                }
            }
        ]
    ],
    "second": [
    ]
}

let smallImg = "", originalImg = "", largeImg = "", mediumImg = "";
jsonObj.main[0].forEach(mainItem => {
  mainItem.content.items.forEach(item => {
    smallImg = item.images.lists.small[0].url;
    originalImg = item.images.lists.original[0].url;
    largeImg = item.images.lists.large[0].url;
    mediumImg = item.images.lists.medium[0].url;
  });
});
console.log(smallImg);
console.log(originalImg);
console.log(largeImg);
console.log(mediumImg);


推荐阅读