0

My code looks like:

  $scope.filters = [{
    name: 'cat1',
    limit: 12 

}, {
    name: 'cat2',
    limit: 12
}];

Can I simply call

limit: varname or $scope.name

does it need to be in a specific format?

3
  • 2
    It is not clear what you are asking. Do you want to add an entry to the array? do you want to access the value of the properties? Commented Jul 7, 2015 at 16:09
  • I'd simply like to change the limit value via a variable rather than hard coded Commented Jul 7, 2015 at 16:10
  • 1
    $scope.filters[0].name Commented Jul 7, 2015 at 16:11

1 Answer 1

1

You have the Array of Javascript Objects that looks like this

[ // array
  { // index 0
      name: 'cat1',
      limit: 12 
  }, 
  { // index 1
      name: 'cat2',
      limit: 12
  }
] // end array

That is assigned to $scope.filters. Now to access or set a specific objects contained in the array you must specify which element using the array index in square brackets.

console.log( $scope.filters[0] ); // {name: 'cat1', limit: 12}

Then you can use a property name to access the properties of one of the objects

var oldLimit = $scope.filters[0].limit; // = 12
$scope.filters[0].limit = oldLimit + 2; // = 14

So to recap $scope is an object, with the sub-element filters that is an array, that contains objects with the properties name and limit.

$scope.filters[index].propertyName
Sign up to request clarification or add additional context in comments.

1 Comment

Glad it was useful! Thanks for selecting my answer!

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.