如何在angularJS中从对象数组中获取数据
How to fetch data from array of objects in angularJS?
这是我的json-
{
topalbums: {
album: [
{
name: "The Very Best of Cher",
playcount: 1634402,
mbid: "a7e2dad7-e733-4bee-9db1-b31e3183eaf5",
url: "http://www.last.fm/music/Cher/The+Very+Best+of+Cher",
artist: {
name: "Cher",
mbid: "bfcc6d75-a6a5-4bc6-8282-47aec8531818",
url: "http://www.last.fm/music/Cher"
}
}
]
}
}
如何通过循环获得数据,艺术家。使用角度控制器的每个即将到来的对象的名称?
试试这个。在控制器中,你可以像这样使用角度foreach。
var app = angular.module("app",[])
app.controller('ctrl',['$scope', function($scope){
$scope.data={
topalbums: {
album: [
{
name: "The Very Best of Cher",
playcount: 1634402,
mbid: "a7e2dad7-e733-4bee-9db1-b31e3183eaf5",
url: "http://www.last.fm/music/Cher/The+Very+Best+of+Cher",
artist: {
name: "Cher",
mbid: "bfcc6d75-a6a5-4bc6-8282-47aec8531818",
url: "http://www.last.fm/music/Cher"
}
},
{
name: "The Very Best of Cher",
playcount: 1634402,
mbid: "a7e2dad7-e733-4bee-9db1-b31e3183eaf5",
url: "http://www.last.fm/music/Cher/The+Very+Best+of+Cher",
artist: {
name: "Cher2",
mbid: "bfcc6d75-a6a5-4bc6-8282-47aec8531818",
url: "http://www.last.fm/music/Cher"
}
}
]
}
};
angular.forEach($scope.data.topalbums,function(album){
angular.forEach(album,function(a){
alert(a.artist.name);
console.log(a.artist.name);
})
})
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div class="item item-checkbox">
<div ng-repeat="album in data.topalbums">
<p ng-repeat="a in album">{{a.artist.name}}</p>
</div>
</div>
假设$scope.allAlbums
中有这个json。
您可以使用迭代所有专辑的艺术家名称
$scope.allAlbums.topalbums.album.forEach(function(item) {
console.log(item.artist.name)
})
您可以试试这个。名称将包含数组相册名称:
var names = data.topalbums.album.map(function(item){
return item.artist.name;
});
相关文章:
- 从json子数组获取数据
- 从javascript数组获取php的日期时间
- 从表中的 JSON 数组获取数据
- 每个表的2D数组获取坐标JTSL,JAVASCRIPT
- javascript从元素到数组获取文本
- 如何从数组获取链接以在youtube Iframe API中播放
- JSON 编码多维数组获取值
- 如何从两个 JSON 数组获取增量
- 通过单击按钮数组获取隐藏 html 元素值的当前数组
- 在 JavaScript 中从内部数组获取数据
- 从数组获取非重复索引在 0 时失败
- JavaScript 从对象中的数组获取数组
- 如何通过 ajax 将 php 数组作为 javascript 数组获取
- 快速从 2D 数组获取索引,无需迭代 jquery
- 从 Nodejs socketio 数组获取所有套接字 id
- 无法动态从 json 数组获取数据
- 如何在javascript中使用数组获取和设置html标签的值
- jQuery对象数组获取一个项作为jQuery对象
- 如何在不向输出数组获取空值的情况下拆分字符串
- 从输入到数组获取数据