I'm working on AngularJs tutorial and I would like to access JSON. I do not understand why in tutorial they use [] to access JSON. When user click on the "a tag" it will put airport.code into setAirport function and get the currentAirport.name back Here is the code:
HTML:
<html ng-app>
<head>
    <title>Demo</title>
    <script type="text/javascript" src="js/lib/angular.min.js"></script>
    <script type="text/javascript" src="js/controllers/app.js"></script>
</head>
<body>
<div ng-controller="AppCtrl">
    <h1>AngulAir</h1>
    <ul>
        <li ng-repeat="airport in airports">
            <a href="" ng-click="setAirport(airport.code)">
                {{airport.code}} - {{airport.city}}
            </a>
        </li>
    </ul>
    <p ng-show="currentAirport">Current Airport : {{currentAirport.name}}</p>
</div>
and this is the JS code:
function AppCtrl ($scope) {
  $scope.airports = {
    "PDX": {
      "code": "PDX",
      "name": "Portland International Airport",
      "city": "Portland",
      "destinations": [
        "LAX",
        "SFO"
      ]
    },
    "STL": {
      "code": "STL",
      "name": "Lambert-St. Louis International Airport",
      "city": "St. Louis",
      "destinations": [
        "LAX",
        "MKE"
      ]
    }
  };
  $scope.currentAirport = null;
  $scope.setAirport = function (code){
    $scope.currentAirport = $scope.airports[code];
  };
}
you can see in the last statement ---> $scope.currentAirport = $scope.airports[code]; it use [] to access the JSON. I do not understand why it is used this way. I tried to experiment in another situation and it seems to work differently, here is my experiment:
HTML:
<html>
<head>
    <title>hello world</title>
</head>
<body>
</body>
<script type="text/javascript" src=jsfile.js></script>
<script type="text/javascript">
alert(airports[code]);
</script>
JS file:
airports = {
    "PDX": {
      "code": "PDX",
      "name": "Portland International Airport",
      "city": "Portland",
      "destinations": [
        "LAX",
        "SFO"
      ]
    },
    "STL": {
      "code": "STL",
      "name": "Lambert-St. Louis International Airport",
      "city": "St. Louis",
      "destinations": [
        "LAX",
        "MKE"
      ]
    }
};
and I get this on my browser "ReferenceError: Can't find variable: code" WHY? the JSON access way of this statement -----> $scope.currentAirport = $scope.airports[code]; is not equal to this statement -----> alert(airports[code]); ?????
why in the second case I can't use [] ? anyone knows please tell me
 
    