0

I wish to access the object '2016-11-12' inside the $scope.list and display the Order and Balance. How could achieve that?

List:

$scope.list = {
  ItemCode:'item1',
  '2016-11-12':[{Order:'1',Balanace:'2'}],
  '2016-11-15':[{Order:' ',Balanace:' '}]
}

I've tried indexof but it seems doesn't work.

$scope.list[0].indexof(1) 

Any idea and suggestion will be great!

Vincent Tang
  • 280
  • 2
  • 13

3 Answers3

0

To access Order value for a given date:

$scope.list['2016-11-12'][0]['Order']

To access Balance value for a given date (you misspelled balance btw):

$scope.list['2016-11-12'][0]['Balance']

n_i_c_k
  • 1,504
  • 10
  • 18
0

Use brackets notation:

$scope.list['2016-11-12'];

See Example:

var app = angular.module('TestApp', []);

app.controller("testCtrl", function ($scope) {
  $scope.list = {
  ItemCode:'item1',
  '2016-11-12':[{Order:'1',Balanace:'2'}],
  '2016-11-15':[{Order:' ',Balanace:' '}]
};
  
  $scope.test = $scope.list['2016-11-12'];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="TestApp">
  <div ng-controller="testCtrl">
    {{test}}
  </div>
</div>
Jaqen H'ghar
  • 16,186
  • 8
  • 49
  • 52
  • You should not answer questions that are this basic. You should search for necessary post and mark it as duplicate – Rajesh Dec 10 '16 at 07:54
  • I tend to agree with you on that one I must say... I had my doubts while posting this answer.. – Jaqen H'ghar Dec 10 '16 at 07:57
0

What you have called $scope.list is actually an object instead of an array as you seem to expect. The '2016-11-12' is a field in that object that has an array value, inside which there is only one object. A more flexible way to construct your object would be to put the objects in an array inside the item object and then access them by index.

$scope.list = {
ItemCode:'item1',
ThingsArray:[
 {Date:'2016-11-12', Order:'1',Balanace:'2'},
 {Date:'2016-11-15', Order:' ',Balanace:' '}
 ]
}

Then you can access them using $scope.list.ThingsArray[0] Note that I have moved the dates into the objects themselves.