0

I have a JSON that has three levels and it's stored as Array I need to get 2nd level(count started from 0) and below in each level has 10+ elements. How it can be implemented on JavaScript.Need your help. All response will be helpful.

ps. some 1st level and 2nd level elements can be empty

[
  {
    "name": "0th level first", //0th level
    "options": [
      {
        "name": "1st level Cafe", // 1st level
        "options": [
          {
            "name": "2nd level Staff", //2nd level
            "options": [
              {
                "name": "Gary", //3rd level
                "age": 18
              },
              {
                "name": "James", //3rd level
                "age": 25
              }
            ]
          }
        ]
      }
    ]
  }
]
  • json you provide is not valid – Kamil Kiełczewski Nov 22 '18 at 14:34
  • @KamilKiełczewski what's wrong with it? – charlietfl Nov 22 '18 at 14:36
  • provide how the output json (result you wan't to get) should look like – Kamil Kiełczewski Nov 22 '18 at 14:37
  • 1
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – str Nov 22 '18 at 14:40
  • can you provide your expected output – Learner Nov 22 '18 at 14:40
  • 1
    I just came up with a solution, that I wouldn't consider the best in terms of readability, and also support (it's experimental) but I just wanted to try out the `flatMap` [function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap), which isn't a part of Node.JS for instance, so be careful of it's support across browsers: `arr.flatMap(firstLevel => firstLevel.options.flatMap(secondLevel => secondLevel.options.flatMap(finalLevel => finalLevel.options)));` – abdullahkady Nov 22 '18 at 14:42
  • sorry, i did't write that some 1st level and 2nd level elements can be empty – Yerkebulan Doroshev Nov 22 '18 at 16:12

3 Answers3

0

Assume data contains your json, we have 2 onliners:

let out = data[0].options[0].options[0];

let outArr = data.flatMap(x=>x.options.flatMap(y=>y.options));

results

// out = {"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}

// outArr = [{"name":"2nd level Staff","options":[{"name":"Gary","age":18},{"name":"James","age":25}]}]

First one (out) contains one 2nd element. We use here tree indexes here (three zeros) first data[0] return first element in array data, then options[0] return first element in options array.

The second solution (outArr) contains array of all 2nd elements. We use here JS flatMap

To write second solution inspired me abdullahkady comment below question.

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
0

Probably this one? :)

  data.forEach((value, index) => {
    for(stage0 in value){
      if(typeof value[stage0] === 'object'){
        value[stage0].forEach((val, index) => {
          for(stage1 in val){
            if(typeof val[stage1] === 'object'){
              val[stage1].forEach((val2, index) => {
                for(stage2 in val2){
                  console.log(val2[stage2]);
                }
              })
            }
          }
        })
      }
    }
  })
shakogele
  • 409
  • 4
  • 14
0

You can use array.forEach which will be only iterating through each level.

on first loop there is a property call options which is an array, you need to loop through the options array in 0 level, on the second loop again one more options array comes you need to again loop through the options array which is 1 level. then you reach the thrid level which is your output.

Third level means starting from zero its second level.

I hope this will solve the issue.

var data = [
  {
    "name": "0th level first", //0th level
    "options": [
      {
        "name": "1st level Cafe", // 1st level
        "options": [
          {
            "name": "2nd level Staff", //2nd level
            "options": [
              {
                "name": "Gary", //3rd level
                "age": 18
              },
              {
                "name": "James", //3rd level
                "age": 25
              }
            ]
          }
        ]
      }
    ]
  }
]

data.forEach(fl => {
 fl.options.forEach(sl => {
  sl.options.forEach(tl => {
    console.log("second level starting from 0",tl)
  })
 })
})
Learner
  • 8,379
  • 7
  • 44
  • 82