我在json中有一个列表,ai需要它具有唯一值
[
{
"equipmentType": "5 MEG DOCSIS",
"status": "On Truck ",
"count": 2
},
{
"equipmentType": "5 MEG DOCSIS",
"status": "Return Faulty",
"count": 3
} ]
我需要使用独特的设备类型:
[
{
"equipmentType": "5 MEG DOCSIS",
"On_TruckCount": 2,
"Return_Faulty": 3
}
]
帮助我找出问题的解决方案 提前谢谢..
修改
我正在尝试但是错了,这里的obj是json obj。
$scope.equipmentDashboard = obj;
$scope.oldEquip="";
$scope.listtest='';
$scope.test = [];
angular.forEach(obj, function(value, key) {
if(value.equipmentType == $scope.oldEquip.equipmentType){
if(value.status=="On Truck ")
$scope.listtest.onTruck = value.count;
else
$scope.listtest.returnFaulty = value.count;
} else {
$scope.test.push($scope.listtest);
$scope.oldEquip = value;
}
});
答案 0 :(得分:1)
对于类似这样的东西,我会使用原生javascript来创建一个临时对象,使用equipmentType
值作为属性键,因为这是数组中对象之间的常见内容。
由于这是所有预处理,我们不需要担心角度范围直到最后。理想情况下,您将在服务中执行此映射,并让服务返回结果
var tmp = {}
data.forEach(function (row) {
if (!tmp[row.equipmentType]) {
tmp[row.equipmentType] = {
equpmentType: row.equipmentType
};
}
if (!tmp[row.equipmentType][row.status] ) {
tmp[row.equipmentType][row.status] = 0;
}
tmp[row.equipmentType][row.status] += row.count;
});
Temp对象将产生:
{
"5 MEG DOCSIS": {
"equpmentType": "5 MEG DOCSIS",
"On Truck ": 2,
"Return Faulty": 3
}
}
然后迭代临时对象,将每个子对象推送到将在$ scope
中使用的数组中var out = [];
for (var key in tmp) {
if (tmp.hasOwnProperty(key)) {
out.push(tmp[key]);
}
}
$scope.equipmentList = out;
的 DEMO 强>