我有json文件如下:
[
{"TYPE": "A", "STATUS": "SUCCESS", "DISCOUNT": 500.00, "TOTAL": 5000.00},
{"TYPE": "A", "STATUS": "SUCCESS", "DISCOUNT": 300.00, "TOTAL": 1266.00},
{"TYPE": "A", "STATUS": "FAIL", "DISCOUNT": 300.00, "TOTAL": 515.00},
{"TYPE": "B", "STATUS": "SUCCESS", "DISCOUNT": 323, "TOTAL": 846.00},
{"TYPE": "B", "STATUS": "FAIL", "DISCOUNT": 80.00, "TOTAL": 3000.00},
{"TYPE": "B", "STATUS": "KIV", "DISCOUNT": 105, "TOTAL": 900.00},
{"TYPE": "C", "STATUS": "KIV", "DISCOUNT": 245.00, "TOTAL": 998.75},
{"TYPE": "B", "STATUS": "SUCCESS", "DISCOUNT": 234.00, "TOTAL": 3500.00},
{"TYPE": "C", "STATUS": "SUCCESS", "DISCOUNT": 201, "TOTAL": 5008.00},
{"TYPE": "C", "STATUS": "FAIL", "DISCOUNT": 712, "TOTAL": 12300.00},
{"TYPE": "A", "STATUS": "SUCCESS", "DISCOUNT": 500.00, "TOTAL": 5000.00},
{"TYPE": "D", "STATUS": "SUCCESS", "DISCOUNT": 300.00, "TOTAL": 1266.00},
{"TYPE": "D", "STATUS": "FAIL", "DISCOUNT": 300.00, "TOTAL": 515.00},
{"TYPE": "D", "STATUS": "SUCCESS", "DISCOUNT": 323, "TOTAL": 846.00},
{"TYPE": "B", "STATUS": "FAIL", "DISCOUNT": 80.00, "TOTAL": 3000.00},
{"TYPE": "B", "STATUS": "KIV", "DISCOUNT": 105, "TOTAL": 900.00},
{"TYPE": "C", "STATUS": "KIV", "DISCOUNT": 245.00, "TOTAL": 998.75},
{"TYPE": "B", "STATUS": "SUCCESS", "DISCOUNT": 234.00, "TOTAL": 3500.00},
{"TYPE": "C", "STATUS": "KIV", "DISCOUNT": 201, "TOTAL": 5008.00},
{"TYPE": "C", "STATUS": "SUCCESS", "DISCOUNT": 712, "TOTAL": 12300.00}
]
如何制作:
示例(预期结果):
1)状态 [“成功”,“失败”,“KIV”]
2)总计 [199000.00,12000.00,6000.00]
3)每种类型的每种状态的数量,例如:
[{A:[{SUCCESS: 2},
{FAIL: 1}]
},
{B:[{SUCCESS:2},
{FAIL:1},
{KIV:1}]
}]
我已经尝试了自己的方式和它的作品(除了没有3)但我想知道它是否可以成为另一种方式更简单。 以下是我的步骤:
var arrStatus=[];
var countStatus=[];
var flagStatus="";
var arrTotal=[];
for (var i = 0; i < data.length; i++) {
var tempStatus = data[i].STATUS;
flagStatus= false;
for(var z = 0; z< arrStatus.length; z++){
if(tempStatus == arrStatus[z]){
arrTotal[z] += data[i].TOTAL;
countStatus[z]++;
flagStatus = true;
}
}
if(flagStatus == false){
countStatus.push(1);
arrStatus.push(tempStatus);
arrTotal.push(data[i].TOTAL);
}
}
感谢您的帮助
答案 0 :(得分:0)
这是一种算法。创建一个地图。键是STATUS值。该值是类型为jsonData(您创建的类)的对象,它具有两个属性:count和total。迭代你的json数据。如果遇到新状态,请创建JsonData类型的新实例并相应地设置其值(例如,count = 1,total = 5000)。如果您遇到已经看到的状态,请将计数增加1,将总计增加新总数(例如,第二次看到SUCCESS条目时计数++和总计=总计+ 1266)。
答案 1 :(得分:0)
那里不是最好的解决方案,但它确实有效!
function getUnique(array, item) {
var items = [];
for (var i = 0, len = array.length; i < len; i++) {
for (var key in array[i]) {
if (key === item) {
items.push(array[i][key]);
}
}
}
return eliminateDuplicates(items);
}
function eliminateDuplicates(arr) {
var i,
len = arr.length,
out = [],
obj = {};
for (i = 0; i < len; i++) {
obj[arr[i]] = 0;
}
for (i in obj) {
out.push(i);
}
return out;
}
var uniqueStatus = getUnique(data, "STATUS");
console.log(uniqueStatus);
输出:[ 'SUCCESS', 'FAIL', 'KIV' ]
function getTotal(array, item) {
var sum = 0;
for (var i = 0, len = array.length; i < len; i++) {
for (var key in array[i]) {
if (array[i][key] === item) {
sum += array[i]["TOTAL"];
}
}
}
return sum;
}
var total = [getTotal(data, "SUCCESS"), getTotal(data, "FAIL"), getTotal(data, "KIV")];
console.log(total);
输出:[ 38532, 19330, 8805.5 ]
var uniqueType = getUnique(data, "TYPE");
console.log(uniqueType);
输出:[ 'A', 'B', 'C', 'D' ]
function makeObject(array, type, status) {
var count = 0;
for (var i = 0, len = array.length; i < len; i++) {
if (array[i]["TYPE"] === type && array[i]["STATUS"] === status) {
count++;
}
}
return count;
}
console.log(makeObject(data, "B", "SUCCESS"));
输出:3