我有一组json值。我想根据所有地理位置对数据进行分组Identified
,Proposal Submitted
,QO under Evaluation
,Negotiation & Contracting
,Closed Lost
,Closed Won
by count
和pipelinevalue
我想将json响应分组为
[{SalesStage: "Identified",count:123,pipelinevalue:8},
{SalesStage: "Proposal Submitted",count:24,pipelinevalue:6},
{SalesStage: "QO under Evaluation",count:11,pipelinevalue:2},
{SalesStage: "Qualified",count:8,pipelinevalue:11},
{SalesStage: "Negotiation & Contracting",count:7,pipelinevalue:16},
{SalesStage: "Closed Lost",count:37,pipelinevalue:2},
{SalesStage: "Closed Won",count:4,pipelinevalue:4}]
const data=[{Geo: "US West", SalesStage: "Identified", count: 34, pipelinevalue: 5},
{Geo: "APAC", SalesStage: "Identified", count: 62, pipelinevalue: 2},
{Geo: "US East", SalesStage: "Identified", count: 27, pipelinevalue: 1},
{Geo: "NSU", SalesStage: "Proposal Submitted", count: 4, pipelinevalue: 1},
{Geo: "US East", SalesStage: "Proposal Submitted", count: 14, pipelinevalue: 4},
{Geo: "US West", SalesStage: "Proposal Submitted", count: 6, pipelinevalue: 1},
{Geo: "US West", SalesStage: "QO under Evaluation", count: 2, pipelinevalue: 1},
{Geo: "US East", SalesStage: "QO under Evaluation", count: 9, pipelinevalue: 1},
{Geo: "NSU", SalesStage: "Qualified", count: 1, pipelinevalue: 6},
{Geo: "US East", SalesStage: "Qualified", count: 1, pipelinevalue: 4},
{Geo: "US West", SalesStage: "Qualified", count: 6, pipelinevalue: 1},
{Geo: "US West", SalesStage: "Negotiation & Contracting", count: 3, pipelinevalue: 8},
{Geo: "US East", SalesStage: "Negotiation & Contracting", count: 4, pipelinevalue: 8} ,
{Geo: "US West", SalesStage: "Closed Lost", count: 37, pipelinevalue: 2},
{Geo: "NSU", SalesStage: "Closed Won", count: 4, pipelinevalue: 4}];
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
最佳答案
干得好 。创建了一个结果obj来存储finalResult,其中key为SalesStage,值为full Obj。
如果结果不具有当前SalesStage的任何属性,请添加它。
如果具有SalesStage的属性,则将当前obj的计数和管道值添加到结果中。
const data=[{Geo: "US West", SalesStage: "Identified", count: 34, pipelinevalue: 5},
{Geo: "APAC", SalesStage: "Identified", count: 62, pipelinevalue: 2},
{Geo: "US East", SalesStage: "Identified", count: 27, pipelinevalue: 1},
{Geo: "NSU", SalesStage: "Proposal Submitted", count: 4, pipelinevalue: 1},
{Geo: "US East", SalesStage: "Proposal Submitted", count: 14, pipelinevalue: 4},
{Geo: "US West", SalesStage: "Proposal Submitted", count: 6, pipelinevalue: 1},
{Geo: "US West", SalesStage: "QO under Evaluation", count: 2, pipelinevalue: 1},
{Geo: "US East", SalesStage: "QO under Evaluation", count: 9, pipelinevalue: 1},
{Geo: "NSU", SalesStage: "Qualified", count: 1, pipelinevalue: 6},
{Geo: "US East", SalesStage: "Qualified", count: 1, pipelinevalue: 4},
{Geo: "US West", SalesStage: "Qualified", count: 6, pipelinevalue: 1},
{Geo: "US West", SalesStage: "Negotiation & Contracting", count: 3, pipelinevalue: 8},
{Geo: "US East", SalesStage: "Negotiation & Contracting", count: 4, pipelinevalue: 8} ,
{Geo: "US West", SalesStage: "Closed Lost", count: 37, pipelinevalue: 2},
{Geo: "NSU", SalesStage: "Closed Won", count: 4, pipelinevalue: 4}];
var result = {};
data.forEach( obj => {
if(result.hasOwnProperty(obj.SalesStage)){
var tempObj = result[obj.SalesStage];
tempObj.count+=obj.count;
tempObj.pipelinevalue+=obj.pipelinevalue;
result[obj.SalesStage]=tempObj;
}else {
result[obj.SalesStage] = obj;
}
})
console.log(Object.values(result))