在AngularJS中,有没有办法知道data.location中有重复,并且每个唯一的data.location只输出一次
In AngularJS, is there a way to know that there is duplicate in the data.location, and only output once for each unique data.location
在AngularJS中,有没有办法知道data.location中有重复项,并且每个唯一的data.location只输出一次。
IE。而不是以下内容:
A Jane
A Tom
B Brian
B Jane
B Mike
R Donald
R Jerry
希望预期输出为:
A Jane
Tom
B Brian
Jane
Mike
R Donald
Jerry
angular代码如下:
<body ng-app="app" ng-controller="ctrl">
<table>
<tr ng-repeat="data in output " >
<td ng-bind="data.location"> </td>
<td ng-bind="data.name"></td>
</tr>
</table>
<script>
angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.output = [
{name: "Jane ", location: "A"},
{name: "Tom", location: "A"},
{name: "Brian", location: "B"},
{name: "Jane", location: "B"},
{name: "Mike", location: "B"},
{name: "Donald", location: "R"},
{name: "Jerry", location: "R"}
] ;
});
angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.output = [{
name: "Jane ",
location: "A"
}, {
name: "Tom",
location: "A"
}, {
name: "Brian",
location: "B"
}, {
name: "Jane",
location: "B"
}, {
name: "Mike",
location: "B"
}, {
name: "Donald",
location: "R"
}, {
name: "Jerry",
location: "R"
}];
var reduced = {};
$scope.output.map(function(item){
reduced[item.location] = reduced[item.location] || [];
reduced[item.location].push(item.name);
});
$scope.reducedOutput = reduced;
});
table, td{
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" ng-controller="ctrl">
<table>
<tr ng-repeat="(key, value) in reducedOutput">
<td ng-bind="key"></td>
<td>
<ul>
<li ng-repeat="val in value" ng-bind="val"></li>
</ul>
</td>
</tr>
</table>
</body>
在AngularJS中,有没有办法知道data.location中有重复项,并且每个唯一的data.location只输出一次。 IE。而不是以下内容:
A Jane
A Tom
B Brian
B Jane
B Mike
R Donald
R Jerry
希望预期输出为:
A Jane
Tom
B Brian
Jane
Mike
R Donald
Jerry
angular代码如下:
<body ng-app="app" ng-controller="ctrl">
<table>
<tr ng-repeat="data in output " >
<td ng-bind="data.location"> </td>
<td ng-bind="data.name"></td>
</tr>
</table>
<script>
angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.output = [
{name: "Jane ", location: "A"},
{name: "Tom", location: "A"},
{name: "Brian", location: "B"},
{name: "Jane", location: "B"},
{name: "Mike", location: "B"},
{name: "Donald", location: "R"},
{name: "Jerry", location: "R"}
] ;
});
angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.output = [{
name: "Jane ",
location: "A"
}, {
name: "Tom",
location: "A"
}, {
name: "Brian",
location: "B"
}, {
name: "Jane",
location: "B"
}, {
name: "Mike",
location: "B"
}, {
name: "Donald",
location: "R"
}, {
name: "Jerry",
location: "R"
}];
var reduced = {};
$scope.output.map(function(item){
reduced[item.location] = reduced[item.location] || [];
reduced[item.location].push(item.name);
});
$scope.reducedOutput = reduced;
});
table, td{
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" ng-controller="ctrl">
<table>
<tr ng-repeat="(key, value) in reducedOutput">
<td ng-bind="key"></td>
<td>
<ul>
<li ng-repeat="val in value" ng-bind="val"></li>
</ul>
</td>
</tr>
</table>
</body>