Soundhar Raj
Soundhar Raj

Reputation: 633

Angular Js - Print array with particular key

Am new to Angular js. I wanted to print an array with particular key.

This is my array.

$scope.airports = {
    'PDX':{
      'code': 'PDX',
      'name': 'PDX airport',
      'destination': [
        'LAX',
        'SFO'
      ]
    },
    'STL':{
      'code':'STL',
      'name':'STL Airport',
      'destination': [
        'TLS',
        'SLT'
      ]
    },

    'MCI':{
      'code':'MCI',
      'name':'MCI Airport',
      'destination':[
        'CMI',
        'IMC'
      ]
    },

  };

The below code prints an array.

{{airports | json}}

The below code is printing the value of STL.

{{airports.STL | json}}

The issue is how to print the value of STL if i assign to a variable like this

In my Controller, I have set the value of airportCode

$scope.airportCode= "STL";

and in my views, i used this code to print. But it's not printing any value, and there is no error in console.

{{airports.airportCode | json}}

Thanks to all.

Upvotes: 0

Views: 216

Answers (1)

A.B
A.B

Reputation: 20445

After storing airpotcode in varaible (ie $scope.airportCode= "STL"; ) you can use this array syntax to access it for $scope.airports

  {{airports[airportCode] | json}}

Note(by @doldt) : $scope.airports is not an array it is an Object

Upvotes: 3

Related Questions