javascript - Ng-repeat 列表,其中包含重复元素的许多值的次数
问题描述
我需要制作一个表格,显示该人的姓名、他的名字在数组中重复的次数,以及每个“颜色”的重复次数。
这是表的数组:
var firstArray = [{
"Name": "John Doe",
"Green": "Yes",
"Pink": "Yes",
"Yellow": "No"
},
{
"Name": "John Doe",
"Green": "Yes",
"Pink": "No",
"Yellow": "No"
},
{
"Name": "Mary",
"Green": "No",
"Pink": "No",
"Yellow": "No"
},
{
"Name": "Mary",
"Green": "No",
"Pink": "Yes",
"Yellow": "No"
},
{
"Name": "Mike",
"Green": "Yes",
"Pink": "Yes",
"Yellow": "Yes"
},
{
"Name": "Mike",
"Green": "No",
"Pink": "No",
"Yellow": "No"
},
{
"Name": "Mary",
"Green": "Yes",
"Pink": "Yes",
"Yellow": "Yes"
}
]
我不知道人们会介绍哪些名字。他们可以多次使用相同的名称,也可以使用完全不同的名称。
我添加了这段代码来计算每个名称重复的次数:
var namesArray = [];
firstArray.forEach(function(e) {
namesArray.push(e.Name);
});
var countNames = namesArray.reduce(function(obj, b) {
obj[b] = ++obj[b] || 1;
return obj;
}, {});
console.log(countNames);
但是在这一步之后,我对如何制作表格完全一无所知,也不知道如何将每个名称的所有绿色、粉色和黄色相加。
有人有更好的方法吗?请帮忙
解决方案
假设您的颜色是恒定的,这里是基本示例:
angular
.module('colors', [])
.controller('colorsCtrl', ['$scope', function($scope) {
let firstArray = [{
"Name": "John Doe",
"Green": "Yes",
"Pink": "Yes",
"Yellow": "No"
}, {
"Name": "John Doe",
"Green": "Yes",
"Pink": "No",
"Yellow": "No"
}, {
"Name": "Mary",
"Green": "No",
"Pink": "No",
"Yellow": "No"
}, {
"Name": "Mary",
"Green": "No",
"Pink": "Yes",
"Yellow": "No"
}, {
"Name": "Mike",
"Green": "Yes",
"Pink": "Yes",
"Yellow": "Yes"
}, {
"Name": "Mike",
"Green": "No",
"Pink": "No",
"Yellow": "No"
}, {
"Name": "Mary",
"Green": "Yes",
"Pink": "Yes",
"Yellow": "Yes"
}];
$scope.colors = {};
firstArray.forEach(function(item, index) {
if ($scope.colors[item.Name]) {
$scope.colors[item.Name].Total += 1;
$scope.colors[item.Name].Green += (item.Green === 'Yes' ? 1 : 0);
$scope.colors[item.Name].Pink += (item.Pink === 'Yes' ? 1 : 0);
$scope.colors[item.Name].Yellow += (item.Yellow === 'Yes' ? 1 : 0);
} else {
$scope.colors[item.Name] = {
'Total': 1,
'Green': (item.Green === 'Yes' ? 1 : 0),
'Pink': (item.Pink === 'Yes' ? 1 : 0),
'Yellow': (item.Yellow === 'Yes' ? 1 : 0)
};
}
});
}]);
.table {
width: 100%;
margin-bottom: 1rem;
color: #212529;
}
.table th,
.table td {
padding: 0.75rem;
vertical-align: middle;
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="colors" ng-controller="colorsCtrl">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Total</th>
<th>Green</th>
<th>Pink</th>
<th>Yellow</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="(key, value) in colors">
<td>{{ key }}</td>
<td>{{ value.Total }}</td>
<td>{{ value.Green }}</td>
<td>{{ value.Pink }}</td>
<td>{{ value.Yellow }}</td>
</tr>
</tbody>
</table>
</div>
推荐阅读
- ruby-on-rails - 防止多次单击创建多个记录 - Rails
- drupal - Drupal 表单模板建议
- java - Eclipse 不解析来自外部 jar 的类
- ios - 如何在 iOS 中为特定的相机视图控制器强制横向方向
- r - 在 R 中加入/匹配数据帧
- git - 无法使用 git 命令将文件推送到 GitHub
- ios - 错误:无法解析构建文件:XCBCore.BuildFile
- java - 在不同线程中具有读取器和写入器的 Java 复制文件(使用 BlockingQueue)
- excel - Excel中的分组和求和
- c++ - C++ 套接字错误 C1083:无法打开包含文件:'unistd.h':没有这样的文件或目录