admin管理员组

文章数量:1397756

How can I retrieve ids from a nested array?
Initially I get such json response and I need to get all ids from all nested arrays. Anybody can help me out here? Should I use filter or any of find functions? An example or explanation could be great.

{
    "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,
                }
            ]
        }
    ]
}

How can I retrieve ids from a nested array?
Initially I get such json response and I need to get all ids from all nested arrays. Anybody can help me out here? Should I use filter or any of find functions? An example or explanation could be great.

{
    "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,
                }
            ]
        }
    ]
}
Share Improve this question edited Jul 6, 2018 at 18:27 LittleJohnny asked Jul 6, 2018 at 18:26 LittleJohnnyLittleJohnny 1052 silver badges13 bronze badges 4
  • which are the ids you want to extract? – lealceldeiro Commented Jul 6, 2018 at 18:29
  • Like 272, 285 - the ones inside – LittleJohnny Commented Jul 6, 2018 at 18:31
  • @LittleJohnny will "id": "a65dd3f0-3fc1-4f93-9123-f5a05ae50703" will be included in ids? – amrender singh Commented Jul 6, 2018 at 18:33
  • No, only the ones in deepest array – LittleJohnny Commented Jul 6, 2018 at 18:35
Add a ment  | 

3 Answers 3

Reset to default 3

Assuming you're looking for the deepest IDs (Run IDs), here is an example of how it can be done.

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() })

Assuming you store that json in some variable, say json,

const ids = json.entries.map(entry => entry.runs.map(run => run.id));

should give you a nested array of the IDs that looks like

[ [ 284, 285, 296 ], [ 272, 273, 274 ] ].

If you wanted this to be in a single dimension list, i.e.

[ 284, 285, 296, 272, 273, 274 ],

you can use concat.apply.

const concatIds = [].concat.apply([], ids);

Filter and find functions would be redundant because you are obtaining all the ids and not specific ones. To solve your problem you can simply just use functions with map . For this example, your object will be called exampleObj in which you can simply do this to get the array of all ids:

exampleObj.entries.map(entryObj => {
  return entryObj.runs.map(runObj=>{return runObj.id});
});

本文标签: javascriptRetrieve data from nested arraysStack Overflow