如何通过索引号访问数组对象中的项? [重复]

时间:2022-02-06 21:21:20

This question already has an answer here:

这个问题在这里已有答案:

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

我希望访问$ scope.list中的对象'2016-11-12'并显示订单和余额。怎么可能实现呢?

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.

我尝试过indexof,但它似乎不起作用。

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

Any idea and suggestion will be great!

任何想法和建议都会很棒!

3 个解决方案

#1


0  

To access Order value for a given date:

要访问给定日期的订单价值:

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

$ scope.list [ '2016年11月12日'] [0] [ '订购']

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

要访问给定日期的余额值(您拼写错误的余额btw):

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

$ scope.list [ '2016年11月12日'] [0] [ '余额']

#2


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>

#3


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实际上是一个对象而不是你想象的数组。 '2016-11-12'是该对象中具有数组值的字段,其中只有一个对象。构造对象的一种更灵活的方法是将对象放在item对象内的数组中,然后通过索引访问它们。

$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.

然后你可以使用$ scope.list.ThingsArray [0]访问它们。注意我已经将日期移动到了对象本身。

#1


0  

To access Order value for a given date:

要访问给定日期的订单价值:

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

$ scope.list [ '2016年11月12日'] [0] [ '订购']

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

要访问给定日期的余额值(您拼写错误的余额btw):

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

$ scope.list [ '2016年11月12日'] [0] [ '余额']

#2


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>

#3


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实际上是一个对象而不是你想象的数组。 '2016-11-12'是该对象中具有数组值的字段,其中只有一个对象。构造对象的一种更灵活的方法是将对象放在item对象内的数组中,然后通过索引访问它们。

$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.

然后你可以使用$ scope.list.ThingsArray [0]访问它们。注意我已经将日期移动到了对象本身。