angular - 从数组中的多个对象添加多个时间,并在 HTML 中使用 Angular 5 中的管道显示
问题描述
在对象数组中,我在嵌套数组中有多个对象,每个对象都有自己的时间值,例如 ""call_duration": "0:21"" ,我想添加所有时间并从 HTML 端以适当的时间格式显示它。数组是
[
{
"phone_number": "1905",
"interactions": [
{
"agentId": "hassan1",
"call_duration": "0:21"
},
{
"agentId": "shabber",
"call_duration": "1:22"
}
]
}
]
我想添加那些通话时间并显示最后的时间,所有的事情都应该在 HTML 中处理
解决方案
仅通过 HTML 很难获得总时间。
我建议你在 javascript 中使用一个函数来获取总时间。如下所示:
HTML
...
<div *ngFor="let item of data ">
Phone: {{item.phone_number}} - Total Duration {{totalDuration(item.interactions)}}
</div>
...
TS
...
totalDuration(interactions: { 'agentId': string, 'call_duration': 'string' }[]) {
let totalDuration = 0;
interactions.forEach(item=>{
// fetch seconds
const seconds = +item.call_duration.split(':')[1];
// fetch minutes and convert them into seconds
const minutes = +item.call_duration.split(':')[0];
const secondsOfMinutes = minutes * 60;
// add all seconds
totalDuration += seconds + secondsOfMinutes;
})
// convert totalDuration to readable format
return Math.floor(totalDuration / 60) + ":" + (totalDuration % 60 ? totalDuration % 60 : '00')
}
...
我还在stackblitz上创建了示例。
推荐阅读
- cordova - 什么决定了 Cordova 中的 android-minSdkVersion?
- c# - 除了最后一项之外,控件中的水平对齐似乎都被忽略了
- angular - 服务器未将 Access-Control-Allow-Origin 响应标头添加到 400 错误响应
- php - 使用 Laravel eloquent 按月或按周分组
- hyperledger-fabric-ca - Hyperledger Fabric CA:x509:证书对 rca-ord 有效,而不是 localhost
- c# - C#中的Android蓝牙扫描仪
- java - TableView JavaFx 中的字符串返回内存地址而不是 ObserveList 中的值
- python - Python - 一致的标签
- java - 学习Java 8
- python - 编译 Python 程序以进行分发