mailman_73 mailman_73 - 6 months ago 27
Javascript Question

iterate keys and values in multiple dictionaries by angularjs

I've got a json file contains few different dictionaries. It looks like this:

{
"variable_1": {
"1357840800": 74,
"1360519200": 71,
"1362938400": 83,
"1365616800": 74,
"1368208800": 78
},
"name": "TITLE",
"variable_2": {
"1357840800": 25817,
"1360519200": 28585,
"1362938400": 26946,
"1365616800": 30034,
"1368208800": 23316
},
"link": "title.html",
"variable_3": {
"1357840800": 1910483,
"1360519200": 2029593,
"1362938400": 2236587,
"1365616800": 2222556,
"1368208800": 1818661
}
}


I want to put the data from
variable_1, variable_2 and variable_3
in different columns of a table. Any variable has the same number of items and the number of them is unknown (it could be 1,2,3,5,8...). The keys are the same in all dictionaries. So new
<td>
should contains only value from another dictionary.

I wrote the code to iterate through one dictionary but I can't find approach to multiple dictionaries.

That's my code for only one dictionary. How should I modify it to handle a few?

<table class="table ng-cloak" ng-if='isActive' ng-repeat="item in data | filter:isActive">
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr ng-repeat='(key, val) in item.variable_1'>
<td>{{ $index }}</td>
<td>{{ 1000*key + 1 | date:'MM.yyyy'}}</td>
<td>{{ val | splitdivisions }}</td>
</tr>
</tbody>
</table>

Answer

If keys in each directory are related to each other, you can simply

<tr ng-repeat='(key, val) in item.variable_1'>
 <td>{{ $index }}</td>
 <td>{{ 1000*key + 1 | date:'MM.yyyy'}}</td>
 <td>{{ val  | splitdivisions }}</td>
 <td>{{  item.variable_2[key] }}</td>
 <td>{{  item.variable_3[key] }}</td>