3

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,
                }
            ]
        }
    ]
}
4
  • which are the ids you want to extract? Commented Jul 6, 2018 at 18:29
  • Like 272, 285 - the ones inside Commented Jul 6, 2018 at 18:31
  • @LittleJohnny will "id": "a65dd3f0-3fc1-4f93-9123-f5a05ae50703" will be included in ids? Commented Jul 6, 2018 at 18:33
  • No, only the ones in deepest array Commented Jul 6, 2018 at 18:35

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.