gpt4 book ai didi

javascript - 对 json 数据进行分组并对值求和

转载 作者:行者123 更新时间:2023-12-01 15:53:49 25 4
gpt4 key购买 nike

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

已确定,Proposal Submitted,QO under Evaluation,Negotiation & Contracting,已关闭,Closed Won by countpipelinevalue

我想将 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,value 是 full Obj。

如果结果没有当前 SalesStage 的任何属性,请添加它。如果它有SalesStage的属性,将当前obj的cou​​nt和pipelinevalue添加到exsiting in result中。

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))

关于javascript - 对 json 数据进行分组并对值求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62990102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com