FanaticTyp
FanaticTyp

Reputation: 185

Loop through nested json object without knowing the names of a specific layer

Is it possible to loop through this nested object, when you don't know how many views ("A", "B", "C"...) this object has? I use currently a alphabetic index, but I heard now the "view layer" can also have different names, so I cant use my current idea, I have to loop through this object without knowing the names of "A", "B", "C" layer. Is there a way?

My current code

loopThroughObject()
{
   let alphabet = ["A", "B", "C"];
   let index = 0;

   this.all_tables.views.forEach(views => {

      views[alphabet[index]].forEach(view => {

         view.positionen.forEach(position=> {
           alert( position.field1);
         });

      });

      index++;
});
}

Nested JSON Object

 all_tables = {  
   "views":[  
      {  
         "A":[  
            {  
               "id":"",
               "username":"",
               "status":"",
               "location":"",
               "positionen":[  
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  }
               ]
            },
            {  
               "id":"",
               "username":"",
               "status":"",
               "location":"",
               "positionen":[  
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  }
               ]
            }
         ],
         "B":[  
            {  
               "id":"",
               "username":"",
               "status":"",
               "location":"",
               "positionen":[  
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  }
               ]
            },
            {  
               "id":"",
               "username":"",
               "status":"",
               "location":"",
               "positionen":[  
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  }
               ]
            }
         ],
         "C":[  
            {  
               "id":"",
               "username":"",
               "status":"",
               "location":"",
               "positionen":[  
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  }
               ]
            },
            {  
               "id":"",
               "username":"",
               "status":"",
               "location":"",
               "positionen":[  
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  },
                  {  
                     "field1":"",
                     "field2":"",
                     "field3":""
                  }
               ]
            }
         ]
      }
   ]
}

Upvotes: 0

Views: 630

Answers (3)

Vishal
Vishal

Reputation: 108

You can use following code

function loopThroughObject()
{
    let index = 0;
    this.all_tables.views.forEach(views => {
       for (var key in views) {
           views[key].forEach(view => {
               view.positionen.forEach(position=> {
                   alert( position.field1);
               });
           });
       }
       index++;
   });
}

Upvotes: 0

Harun Yilmaz
Harun Yilmaz

Reputation: 8558

I think your JSON structure is not totally correct.

First, the views property is array but it has 1 item in it and the only item of it is object that has view items. I think it should be an object in the first place since JavaScript does not have any associative array type.

"views": {  
         "A":[ .. ],
         "B":[ .. ],
         ...
}

Then you can iterate with Object methods like Object.keys() or Object.values() or Object.entries() etc.

Object.values(this.all_tables.views).forEach(viewGroup => {

     viewGroup.forEach(view => {

         view.positionen.forEach(position=> {
           alert( position.field1);
         });
     });
});

For further reading:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Methods_of_the_Object_constructor

Upvotes: 1

Cerberus
Cerberus

Reputation: 10218

There's several Object methods for this: Object.keys (since ES5), Object.values (since ES2017), Object.entries (still experimental). For the most general approach, you could use the first one:

function loopThroughObject() {
  this.all_tables.views.forEach(views => {

    Object.keys(views).forEach(key => {
      let view = views[key];

      view.positionen.forEach(position => {
        alert(position.field1);
      });

    });
  });
}

Upvotes: 2

Related Questions