arrays - 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。
非常感谢您的帮助。
解决方案
看起来您的主数组有一个数组作为第一个元素。如果这是正确的,您将获取 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);
推荐阅读
- flutter - Page transitions issue [Flutter]
- xamarin - Xamarin App - Can't deploy to Android 11 emulator
- html - 超过父身高时如何匹配兄弟身高
- r - 如何在 R 中使用共享参数执行非线性最小二乘?
- php - 在 Laravel 上保存后如何获取正确的 Id 参数
- css - 有没有办法在不使用 className 的情况下使用 ID 在 React 中设置元素的样式?
- c++ - constexpr 字符串文字检查:语法短,没有运行时的可能性
- javascript - Discord.js - CommandFile.Run() 不是函数
- docker - 使用热重载进行调试
- mysql - 将我的节点 js 应用程序从 heroku 连接到 Godaddy 上的 Mysql 数据库