0

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.

1
  • Important note: that is not an array, that is an object. Commented Mar 25, 2015 at 10:33

1 Answer 1

3

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

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

5 Comments

@SoundharRaj glad it helped you, you can upvote and accept it as an answer so it may help others as well :)
Let's emphasise though that $scope.airports is not an array in this case, but an object.
yes, it says i have to wait for 5 more mins to accept
@doldt thanks for pointing, but i havent said its array, i said array like syntax :)
@A.B It wasn't a problem with your wording, but OP's, and I think the accepted answer should point that out, because the distinction is important.

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.