形成新的对象数组并获取相似值的计数

时间:2017-05-08 17:39:55

标签: javascript json

我正在尝试使用旧的数组对象数据形成新的对象数组。我想得到成功和失败值的平均值。

//Old array of objects

[{
  "id": "1",
  "month": "MAR",
  "description": "success",
  "count": "100"
}, {
  "id": "2",
  "month": "APRIL",
  "description": "success",
  "count": "110"
}, {
  "id": "3",
  "month": "MAR",
  "description": "failed",
  "count": "50"
}, {
  "id": "4",
  "month": "MAR",
  "description": "failed",
  "count": "20"
}, {
  "id": "5",
  "month": "APRIL",
  "description": "success",
  "count": "100"
}, {
  "id": "6",
  "month": "APRIL",
  "description": "failed",
  "count": "80"
},
{
   "id": "7",
   "month": "MAR",
   "description": "success",
   "count": "300"
},
 {
   "id": "8",
   "month": "APRIL",
   "description": "failed",
   "count": "40"
}
]

新的对象数组,以获得成功的平均值,并在每个月失败

//new array of objects
[{
"month":"MAR",
"success":200        // 100+300/2
"failed":35          // 50+20/2
},
{
"month":"APRIL",
"success":105        // 100+110/2
"failed":60          // 40+80/2  
}]

我试图获得独特的月份和数据计数,但我无法形成确切的预期输出。任何帮助都将非常感激。

JSFIDDLE

2 个答案:

答案 0 :(得分:2)

以下是如何使用ES6以函数式编程样式执行此操作:

function summary(data) {
    return Array.from(new Set(data.map(o => o.month)), month =>  
        ["failed", "success"].reduce( (acc, description) => {
            const match = acc[1].filter(o => o.description == description)
                                .map(o => +o.count);
            acc[0][description] = match.length 
                               && match.reduce((a,b) => a+b)/match.length;
            return acc;
        }, [{ month }, data.filter(o => o.month == month)] )[0]
    );
}

const data = [{
  "id": "1",
  "month": "MAR",
  "description": "success",
  "count": "100"
}, {
  "id": "2",
  "month": "APRIL",
  "description": "success",
  "count": "110"
}, {
  "id": "3",
  "month": "MAR",
  "description": "failed",
  "count": "50"
}, {
  "id": "4",
  "month": "MAR",
  "description": "failed",
  "count": "20"
}, {
  "id": "5",
  "month": "APRIL",
  "description": "success",
  "count": "100"
}, {
  "id": "6",
  "month": "APRIL",
  "description": "failed",
  "count": "80"
}, {
   "id": "7",
   "month": "MAR",
   "description": "success",
   "count": "300"
}, {
   "id": "8",
   "month": "APRIL",
   "description": "failed",
   "count": "40"
}];

const result = summary(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

Vanilla旧式Javascript方法:

var list = [{
  "id": "1",
  "month": "MAR",
  "description": "success",
  "count": "100"
}, {
  "id": "2",
  "month": "APRIL",
  "description": "success",
  "count": "110"
}, {
  "id": "3",
  "month": "MAR",
  "description": "failed",
  "count": "50"
}, {
  "id": "4",
  "month": "MAR",
  "description": "failed",
  "count": "20"
}, {
  "id": "5",
  "month": "APRIL",
  "description": "success",
  "count": "100"
}, {
  "id": "6",
  "month": "APRIL",
  "description": "failed",
  "count": "80"
},
{
   "id": "7",
   "month": "MAR",
   "description": "success",
   "count": "300"
},
 {
   "id": "8",
   "month": "APRIL",
   "description": "failed",
   "count": "40"
}
];

var d = {};

for (var i=0,l; l = list[i]; i++) {
	if (!d[l.month]) d[l.month] = {failed:0, fcount:0, success:0, scount:0};

  if (l.description == 'failed') {
  	var c = d[l.month].fcount;
  	d[l.month].fcount++;
    d[l.month].failed = d[l.month].failed * c / (c + 1) + l.count / (c + 1);
  } else if (l.description == 'success') {
  	var c = d[l.month].scount;
  	d[l.month].scount++;
    d[l.month].success = d[l.month].success * c / (c + 1) + l.count / (c + 1);
  }
}
console.log(d);