遍历角度控制器中的嵌套 json 数组并获取唯一值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26139473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate through nested json array in angular controller and get unique values
提问by vitalym
I need to iterate through nested json array which looks like that
我需要遍历嵌套的 json 数组,它看起来像这样
[
{
"title": "EPAM",
"technologies": [
"PHP",
".net",
"Java",
"Mobile",
"Objective-C",
"Python",
"Ruby"
],
"location": "Belarus",
"city": "Minsk"
},
{
"title": "Parallels",
"technologies": [
"PHP",
"Java",
"C++",
"iOS Development",
"C#",
"Ember.js"
],
"location": "Russia",
"city": "Moscow"
}
]
What I want is to iterate through list of technologies in each company and then return a list of unique values. I failed, however, to access a single company in company arrays in the controller. It looks like this so far
我想要的是遍历每个公司的技术列表,然后返回一个唯一值列表。但是,我无法访问控制器中公司阵列中的单个公司。到目前为止看起来像这样
var app = angular.module('app', []);
app.controller('CompaniesController', ['$scope', '$http',
function($scope, $http) {
$http.get('json/companies.json').success(function(data) {
$scope.companies = data; // get data from json
$scope.techStack = []
$scope.companies = data.query(function(companies) {
console.log(companies); //I expected to see data here
});
});
}
]);
Apparently I'm doing something wrong.
显然我做错了什么。
回答by user3681587
Use angular's forEach:
使用 angular 的 forEach:
var app = angular.module('app', []);
app.controller('CompaniesController', ['$scope', '$http',
function($scope, $http) {
$http.get('json/companies.json').success(function(data) {
$scope.companies = data; // get data from json
angular.forEach($scope.companies, function(item){
console.log(item.technologies);
})
});
});
}
]);
回答by Evgeny
If you need only to display nested array on UI, you can do that straight in view e.g.
如果您只需要在 UI 上显示嵌套数组,您可以直接在视图中执行,例如
<tr ng-repeat="i in Items">
<td valign="top">{{i.Value}}</td>
<td valign="top">
<table>
<tr ng-repeat="c in i.Children">
<td>{{c.Value}}</td>
</tr>
</table>
</td>
</tr>
回答by ebram khalil
In order to loop through array in AngularJS, you can simply use angular.forEach. For example,
为了在 AngularJS 中循环数组,你可以简单地使用angular.forEach。例如,
angular.forEach(companiesList, function(company) {
//Here you can access each company.
});
I have made a simple demo based on your code that list "Companies" and unique"Technologies".
我根据您列出“公司”和独特“技术”的代码做了一个简单的演示。
回答by Sunil Kumar
try this
尝试这个
var app = angular.module('app', []);
app.controller('CompaniesController', ['$scope', '$http',
function($scope, $http) {
$http.get('json/companies.json').success(function(data) {
$scope.companies = data; // get data from json
$scope.techStack = []
angular.forEach($scope.companies, function(item){
$scope.techStack = item.technologies;
var uniqueTech = [];
for (var i = 0; i < $scope.techStack.length; i++)
{
if (uniqueTech.indexOf($scope.techStack[i]) == -1)
uniqueTech.push($scope.techStack[i]);
}
console.log("Unique Technologies : " + uniqueTech);
})
});
}
]);
回答by vishal shah
user angular's foreach function for looping through data.
用户 angular 的 foreach 函数用于循环数据。

