0

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":""
                  }
               ]
            }
         ]
      }
   ]
}
1
  • is that even possible? :( Commented Jun 20, 2019 at 7:16

3 Answers 3

2

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

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

1 Comment

Awesome, I have used exactly ur example :)
1

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

1 Comment

Thanks, you are right the json structure was indeed not correctly. I thought I need them as "array".
0

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

1 Comment

Thanks, thats a way without using the modern technics ;)

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.