从嵌套数组中检索数据
Retrieve data from nested arrays
如何从嵌套数组中检索 ID?
最初我得到这样的 json 响应,我需要从所有嵌套数组中获取所有 ID。
有人可以帮我吗?
我应该使用过滤器还是任何查找功能?
一个例子或解释可能很棒。
{
"id": 271,
"name": "anything",
"description": null,
"entries": [
{
"id": "fda2afe0-dfc4-4373-9e50-8b140a46f25e",
"name": "first occurence",
"runs": [
{
"id": 284,
"name": "the element from which I want to get id",
"description": null,
"created_on": 1530627823,
"created_by": 2
},
{
"id": 285,
"name": "element for id 2",
"created_by": 2
},
{
"id": 296,
"name": "element for id 3",
"created_on": 1530710993,
"created_by": 2
}
]
},
{
"id": "a65dd3f0-3fc1-4f93-9123-f5a05ae50703",
"name": "second occurence",
"runs": [
{
"id": 272,
"name": "element for id 4",
"created_by": 2,
},
{
"id": 273,
"created_by": 2,
},
{
"id": 274,
"created_by": 2,
}
]
}
]
}
假设您将 json 存储在某个变量中,比如 json
,
const ids = json.entries.map(entry => entry.runs.map(run => run.id));
应该给你一个嵌套的 ID 数组,看起来像
[ [ 284, 285, 296 ], [ 272, 273, 274 ] ]
.
如果您希望它位于单一维度列表中,即
[ 284, 285, 296, 272, 273, 274 ]
,
const concatIds = [].concat.apply([], ids);
假设您正在寻找最深的 ID(运行 个 ID),这里是一个如何完成的示例。
let response = {"id":271,"name":"anything","description":null,"entries":[{"id":"fda2afe0-dfc4-4373-9e50-8b140a46f25e","name":"first occurence","runs":[{"id":284,"name":"the element from which I want to get id","description":null,"created_on":1530627823,"created_by":2},{"id":285,"name":"element for id 2","created_by":2},{"id":296,"name":"element for id 3","created_on":1530710993,"created_by":2}]},{"id":"a65dd3f0-3fc1-4f93-9123-f5a05ae50703","name":"second occurence","runs":[{"id":272,"name":"element for id 4","created_by":2,},{"id":273,"created_by":2,},{"id":274,"created_by":2,}]}]}
function getRunIDs() {
let entries = response['entries'] || []
let runIDs = []
entries.forEach(entry => {
entry['runs'].forEach(run => {
runIDs.push(run['id'])
})
})
return runIDs
}
console.log({ runIDs: getRunIDs() })
过滤和查找功能是多余的,因为您获取的是所有 ID,而不是特定的 ID。要解决您的问题,您只需使用带有 map
的函数即可。对于此示例,您的对象将被称为 exampleObj
,您可以在其中简单地执行此操作以获取所有 ID 的数组:
exampleObj.entries.map(entryObj => {
return entryObj.runs.map(runObj=>{return runObj.id});
});
如何从嵌套数组中检索 ID?
最初我得到这样的 json 响应,我需要从所有嵌套数组中获取所有 ID。
有人可以帮我吗?
我应该使用过滤器还是任何查找功能?
一个例子或解释可能很棒。
{
"id": 271,
"name": "anything",
"description": null,
"entries": [
{
"id": "fda2afe0-dfc4-4373-9e50-8b140a46f25e",
"name": "first occurence",
"runs": [
{
"id": 284,
"name": "the element from which I want to get id",
"description": null,
"created_on": 1530627823,
"created_by": 2
},
{
"id": 285,
"name": "element for id 2",
"created_by": 2
},
{
"id": 296,
"name": "element for id 3",
"created_on": 1530710993,
"created_by": 2
}
]
},
{
"id": "a65dd3f0-3fc1-4f93-9123-f5a05ae50703",
"name": "second occurence",
"runs": [
{
"id": 272,
"name": "element for id 4",
"created_by": 2,
},
{
"id": 273,
"created_by": 2,
},
{
"id": 274,
"created_by": 2,
}
]
}
]
}
假设您将 json 存储在某个变量中,比如 json
,
const ids = json.entries.map(entry => entry.runs.map(run => run.id));
应该给你一个嵌套的 ID 数组,看起来像
[ [ 284, 285, 296 ], [ 272, 273, 274 ] ]
.
如果您希望它位于单一维度列表中,即
[ 284, 285, 296, 272, 273, 274 ]
,
const concatIds = [].concat.apply([], ids);
假设您正在寻找最深的 ID(运行 个 ID),这里是一个如何完成的示例。
let response = {"id":271,"name":"anything","description":null,"entries":[{"id":"fda2afe0-dfc4-4373-9e50-8b140a46f25e","name":"first occurence","runs":[{"id":284,"name":"the element from which I want to get id","description":null,"created_on":1530627823,"created_by":2},{"id":285,"name":"element for id 2","created_by":2},{"id":296,"name":"element for id 3","created_on":1530710993,"created_by":2}]},{"id":"a65dd3f0-3fc1-4f93-9123-f5a05ae50703","name":"second occurence","runs":[{"id":272,"name":"element for id 4","created_by":2,},{"id":273,"created_by":2,},{"id":274,"created_by":2,}]}]}
function getRunIDs() {
let entries = response['entries'] || []
let runIDs = []
entries.forEach(entry => {
entry['runs'].forEach(run => {
runIDs.push(run['id'])
})
})
return runIDs
}
console.log({ runIDs: getRunIDs() })
过滤和查找功能是多余的,因为您获取的是所有 ID,而不是特定的 ID。要解决您的问题,您只需使用带有 map
的函数即可。对于此示例,您的对象将被称为 exampleObj
,您可以在其中简单地执行此操作以获取所有 ID 的数组:
exampleObj.entries.map(entryObj => {
return entryObj.runs.map(runObj=>{return runObj.id});
});