Javascript 将json数据分组并求和值

Javascript 将json数据分组并求和值,javascript,jquery,json,Javascript,Jquery,Json,我有一组json值。我想根据所有地理位置对数据进行分组 确定,提交的建议书,评估中的质量保证,谈判与合同,已赔付,已赔付通过计数和管道价值 我想将json响应分组为 [{SalesStage: "Identified",count:123,pipelinevalue:8}, {SalesStage: "Proposal Submitted",count:24,pipelinevalue:6}, {SalesStage: "QO under E

我有一组json值。我想根据所有地理位置对数据进行分组

确定
提交的建议书
评估中的质量保证
谈判与合同
已赔付
已赔付
通过
计数
管道价值

我想将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:“美国西部”,salestage:“已识别”,计数:34,管道值:5},
{Geo:“亚太地区”,SalesStage:“已识别”,计数:62,管道价值:2},
{Geo:“美国东部”,SalesStage:“已识别”,计数:27,管道值:1},
{Geo:“NSU”,SalesStage:“提案已提交”,计数:4,管道值:1},
{Geo:“美国东部”,SalesStage:“提案已提交”,计数:14,管道价值:4},
{Geo:“美国西部”,SalesStage:“提案已提交”,计数:6,管道价值:1},
{Geo:“美国西部”,SalesStage:“评估中的质量”,计数:2,管道价值:1},
{Geo:“美国东部”,SalesStage:“评估中的质量”,计数:9,管道价值:1},
{Geo:“NSU”,SalesStage:“合格”,计数:1,管道值:6},
{Geo:“美国东部”,SalesStage:“合格”,计数:1,管道值:4},
{Geo:“美国西部”,SalesStage:“合格”,计数:6,管道值:1},
{Geo:“美国西部”,SalesStage:“谈判与承包”,计数:3,管道价值:8},
{Geo:“美国东部”,SalesStage:“谈判与承包”,计数:4,管道价值:8},
{Geo:“美国西部”,SalesStage:“关闭丢失”,计数:37,管道价值:2},
{Geo:“NSU”,SalesStage:“已结算韩元”,计数:4,管道值:4}]

给你。创建一个结果obj以存储最终结果,其中键为SalesStage,值为full obj

如果结果不具有当前SalesStage的任何属性,请添加它。 如果具有SalesStage属性,则将当前obj的计数和pipelinevalue添加到现有输入结果中

const data=[{Geo:“美国西部”,salestage:“已识别”,计数:34,管道值:5},
{Geo:“亚太地区”,SalesStage:“已识别”,计数:62,管道价值:2},
{Geo:“美国东部”,SalesStage:“已识别”,计数:27,管道值:1},
{Geo:“NSU”,SalesStage:“提案已提交”,计数:4,管道值:1},
{Geo:“美国东部”,SalesStage:“提案已提交”,计数:14,管道价值:4},
{Geo:“美国西部”,SalesStage:“提案已提交”,计数:6,管道价值:1},
{Geo:“美国西部”,SalesStage:“评估中的质量”,计数:2,管道价值:1},
{Geo:“美国东部”,SalesStage:“评估中的质量”,计数:9,管道价值:1},
{Geo:“NSU”,SalesStage:“合格”,计数:1,管道值:6},
{Geo:“美国东部”,SalesStage:“合格”,计数:1,管道值:4},
{Geo:“美国西部”,SalesStage:“合格”,计数:6,管道值:1},
{Geo:“美国西部”,SalesStage:“谈判与承包”,计数:3,管道价值:8},
{Geo:“美国东部”,SalesStage:“谈判与承包”,计数:4,管道价值:8},
{Geo:“美国西部”,SalesStage:“关闭丢失”,计数:37,管道价值:2},
{Geo:“NSU”,SalesStage:“已结算韩元”,计数:4,管道值:4}];
var result={};
data.forEach(obj=>{
if(result.hasOwnProperty(obj.salestage)){
var tempObj=结果[obj.salestage];
临时对象计数+=对象计数;
tempObj.pipelinevalue+=obj.pipelinevalue;
结果[obj.salestage]=tempObj;
}否则{
结果[obj.salestage]=obj;
}
})

console.log(Object.values(result))
使用
forEach

const data=[{Geo:“美国西部”,salestage:“已识别”,count:34,pipelinevalue:5},{Geo:“亚太地区”,salestage:“已识别”,count:62,pipelinevalue:2},{Geo:“美国东部”,salestage:“已识别”,count:27,pipelinevalue:1},{Geo:“美国东部”,salestage:“提案已提交”,计数:14,管道价值:4},{Geo:“美国西部”,销售阶段:“提案已提交”,计数:6,管道价值:1},{Geo:“美国西部”,销售阶段:“评估中的质量”,计数:2,管道价值:1},{Geo:“美国东部”,销售阶段:“评估中的质量”,计数:9,管道价值:1},{Geo:“NSU”,销售阶段:“合格”“,count:1,pipelinevalue:6},{Geo:“美国东部”,salestage:“合格”,count:1,pipelinevalue:4},{Geo:“美国西部”,salestage:“合格”,count:6,pipelinevalue:1},{Geo:“美国西部”,salestage:“谈判与承包”,count:3,pipelinevalue:8},{Geo:“美国东部”,salestage:“谈判与承包”,count:4,pipelinevalue:8},{Geo:“美国西部”,SalesStage:“封闭损失”,计数:37,管道价值:2},{Geo:“NSU”,SalesStage:“封闭赢得”,计数:4,管道价值:4}];
变量计数={}
data.forEach(o=>{
常量{salestage,count,pipelinevalue}=o
如果(!counts[o.SalesStage]){counts[o.SalesStage]={SalesStage:SalesStage,count:count,pipelinevalue:pipelinevalue}}
else{counts[o.SalesStage]={SalesStage:SalesStage,count:count+counts[o.SalesStage]。count,pipelinevalue:pipelinevalue+counts[o.SalesStage]。pipelinevalue}
})

console.log(Object.values(counts))
尝试运行下面的代码。我为最终响应创建了一个名为“sales_stage”的json对象。希望它能解决您的问题

const data=[{Geo:“美国西部”,salestage:“已识别”,count:34,pipelinevalue:5},{Geo:“亚太地区”,salestage:“已识别”,count:62,pipelinevalue:2},{Geo:“美国东部”,salestage:“已识别”,count:27,pipelinevalue:1},{Geo:“美国东部”,salestage:“提案已提交”,计数:14,管道价值:4},{Geo:“美国西部”,销售阶段:“提案已提交”,计数:6,管道价值:1},{Geo:“美国西部”,销售阶段:“评估中的质量”,计数:2,管道价值:1},{Geo:“美国东部”,销售阶段:“评估中的质量”