如何使用过滤器对数组中的对象进行分组
How group objects in array from ng-repeat with filter?
如何使用过滤器对 ng-repeating 数组中的对象进行分组?
我有一个带有对象的数组,我想按其国家/地区对此对象进行分组。
示例:我想要这个结果:
Free : Australia, India, United States
Pay : Australia
Not Pay : Australia, India
从:
$scope.lists = [{
"id": 1
"field": [
{
country: "Australia",
type: "Free"
},
{
country: "Australia",
type: "Pay"
},
{
country: "Australia",
type: "Not Pay"
},
{
country: "India",
type: "Free"
},
{
country: "India",
type: "Not Pay"
},
{
country: "United States",
type: "free"
},
},
{
"id": 2
"field": [
{
country: "Australia",
type: "Pay"
},
{
country: "India",
type: "Free"
},
{
country: "India",
type: "Not Pay"
}
}
]
我用代码尝试了这个:
<div ng-repeat="list in lists">
<ul ng-repeat="(key, value) in list.field | groupBy: 'type'">
{{ key }}
<li ng-repeat="country in value">
: {{ country }}
</li>
</ul>
</div>
解决:
我使用角度 1.4.9 和角度滤波器 0.5.7
<div ng-repeat="list in lists">
<ul ng-repeat="(key, value) in list.field | groupBy: 'type'">
{{ key }}
<li ng-repeat="item in value">
: {{ item.country }}
</li>
</ul>
</div>
我创建了Plunker.请检查一下。
我用过角度1.2.20
和angular-filter.min.js
我没有更改HTML和JS的任何部分。它对我来说工作正常。
.JS:
var app = angular.module('app', ['angular.filter']);
app.controller('MainController', function($scope) {
$scope.lists = [{
"id": 1,
"field": [
{
country: "Australia",
type: "Free"
},
{
country: "Australia",
type: "Pay"
},
{
country: "Australia",
type: "Not Pay"
},
{
country: "India",
type: "Free"
},
{
country: "India",
type: "Not Pay"
},
{
country: "United States",
type: "Free"
},
],
},
]
});
.HTML:
<body ng-controller="MainController">
<ul ng-repeat="(key, value) in lists[0].field | groupBy: 'type'">
{{ key }}
<li ng-repeat="country in value">
: {{ country }}
</li>
</ul>
</body>
更新的网页
<div ng-repeat="list in lists">
<ul ng-repeat="(key, value) in list.field | groupBy: 'type'">
{{ key }}
<li ng-repeat="country in value">
: {{ country }}
</li>
</ul>
</div>
更新的普伦克
您在scope
object
和ng-repeat
object
中具有相同的对象country
。将country
更改为item.country
检查角度过滤器
var app = angular.module('app', ['angular.filter']);
app.controller('MainController', function($scope) {
var field = [{
"id": 1,
"field": [
{
country: "Australia",
type: "Free"
},
{
country: "Australia",
type: "Pay"
},
{
country: "Australia",
type: "Not Pay"
},
{
country: "India",
type: "Free"
},
{
country: "India",
type: "Not Pay"
},
{
country: "United States",
type: "Free"
},
],
},
{
"id": 2,
"field": [
{
country: "Australia",
type: "Free1"
},
{
country: "Australia",
type: "Pay1"
},
{
country: "Australia",
type: "Not Pay1"
},
{
country: "India",
type: "Free1"
},
{
country: "India",
type: "Not Pay1"
},
{
country: "United States",
type: "Free1"
},
],
}
];
$scope.lists = field;
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.9/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.8/angular-filter.min.js"></script>
<div ng-app="app" ng-controller="MainController">
<div ng-repeat="list in lists">
<ul ng-repeat="(key, value) in list.field | groupBy: 'type'">
{{ key }}
<li ng-repeat="item in value">
: {{ item.country }}
</li>
</ul>
</div>
</div>
代码
中的一点更正,它工作正常
<div ng-app="myApp" ng-controller="myCountries">
<ul ng-repeat="(key, value) in field | groupBy: 'type'">
{{ key }}
<li ng-repeat="bundle in value">
: {{ bundle.country }}
</li>
</ul>
这是一个小提琴工作
相关文章:
- 发送json对象或使用express路由呈现视图
- 在使用object.create创建的对象中使用super
- 如何在JavaScript中创建对象时使用变量的值
- Javascript在JSON中查找对象(我使用Angular)
- 使用sinon.js创建一个“;“间谍对象”;使用基于真实构造函数/原型的间谍方法
- HTML 对象在使用 ContentPlaceHolder 时在 Javascript 中显示为未定义
- 允许具有相同 id 的对象可以使用 jQuery 拖动
- 在选择对象中使用多个 add() 命令
- 在加载后创建的对象上使用 jQuery 选择器
- 在带有嵌入的对象中使用JavaScript刷新视频
- 如何将Underscore.js过滤器与对象一起使用
- 在 Javascript 对象中使用 HTML5 音频
- 如何在对象上使用trim函数来删除空白jquery
- 我该如何访问由gullow灰质插件创建的数据对象(当使用nunjucks编译html文件时)
- Javascript:当属性可能不存在时,在对象中使用OR条件
- 在多级json对象中使用变量作为键
- 在多个子对象上使用每个函数
- 为什么'这'与函数对象一起使用时会有不同的处理方式
- 如何在其他对象中动态生成javascript对象.你能在对象内部使用for循环吗
- 使用“new”关键字创建的对象和使用“Object.create”创建的对象给出不同的结果