Naguib Ihab Naguib Ihab - 2 months ago 6
JSON Question

Can't get JSON object from a two dimensional array. AngularJS

In my directive I am calling a function that's returning me the content of a JSON file, the content looks like this:

{
"UpdatedBy" : "Naguib",
"UpdatedOn" : "29/09/2016",
"UpdatedFrom": "data.doc",
"100":
[
{
"title":"Class View",
"overview":"TBC",
"menuItem":"TBC",
"UpdatedBy" : "Naguib",
"UpdatedOn" : "29/09/2016"
}
],
"101":
[
{
"title":"Time Table",
"overview":"TBC",
"menuItem":"TBC",
"UpdatedBy" : "Naguib",
"UpdatedOn" : "29/09/2016"
}
]
}


The function is working fine and returning me a JSON array in
$scope.helptext

So when I call
helptext
from HTML and pass the index of the first dimention it returns an array with the data but I can't get any data in the nested array:

<pre>
ONE
{{helptext[101]}}
TWO
{{helptext[101].menuItem}}
THREE
{{helptext[101][menuItem]}}
FOUR
{{helptext[101]['menuItem']}}
</pre>


returns


The html that is generated is exactly how it looks:

<pre class="ng-binding">
ONE
[{"title":"Time Table","overview":"TBC","menuItem":"TBC","UpdatedBy":"Naguib","UpdatedOn":"29/09/2016"}]
TWO

THREE

FOUR

</pre>


I am fairly new to Angular but I can't figure out how I can call the nested array.

Any help is appreciated!

Answer

You need to access each of them like an array: {{helptext[101][0].menuItem}}

If you need to display each possible menuItem nested within those 100, 101, etc. arrays, look into using ngRepeat

Something like this should help get you started:

<pre ng-repeat="menuData in helptext[101]">{{menuData.menuItem}}<pre>
Comments