Фильтрация массива объектов по группе на основе значения свойства - данные из ответа JSON - PullRequest
0 голосов
/ 14 июля 2020

У меня есть данные json, которые я хочу создать в таблице на основе ответа json. Я хочу сгруппировать значение на основе SalesStage. В группе 1 есть данные только для Qualified ,Proposal Submitted and Negotiation & Contracting. В группе 2 есть только данные Closed Won, В группе 3 Closed lost,Deferred и в группе 4 Identified,QO under Evaluation

[{Geo: "US West", SalesStage: "Identified", count: 24, pipelinevalue: 3140000},
 {Geo: "US East", SalesStage: "Identified", count: 22, pipelinevalue: 1303344},
 {Geo: "NSU", SalesStage: "Proposal Submitted", count: 2, pipelinevalue: 554000},
 {Geo: "US East", SalesStage: "Proposal Submitted", count: 12, pipelinevalue: 4516510},
 {Geo: "NSU", SalesStage: "Qualified", count: 1, pipelinevalue: 60000},
 {Geo: "US West", SalesStage: "Proposal Submitted", count: 5, pipelinevalue: 977914},
 {Geo: "US East", SalesStage: "Qualified", count: 1, pipelinevalue: 480000},
 {Geo: "US West", SalesStage: "Qualified", count: 5, pipelinevalue: 1084400},
 {Geo: "US East", SalesStage: "Closed Won", count: 30, pipelinevalue: 11725971},
 {Geo: "US West", SalesStage: "Closed Won", count: 7, pipelinevalue: 937536},
 {Geo: "US West", SalesStage: "Closed Lost", count: 91, pipelinevalue: 9799084},
 {Geo: "US East", SalesStage: "Deferred Opportunity", count: 26, pipelinevalue: 1858042},
 {Geo: "US West", SalesStage: "Deferred Opportunity", count: 3, pipelinevalue: 777000}]

Ожидаемый результат group1 =

[{Geo: "NSU", SalesStage: "Qualified", count: 1, pipelinevalue: 60000},
 {Geo: "US East", SalesStage: "Qualified", count: 1, pipelinevalue: 480000},
 {Geo: "US West", SalesStage: "Qualified", count: 5, pipelinevalue: 1084400},
 {Geo: "NSU", SalesStage: "Proposal Submitted", count: 2, pipelinevalue: 554000},
 {Geo: "US West", SalesStage: "Proposal Submitted", count: 5, pipelinevalue: 977914}]

group2=[{Geo: "US East", SalesStage: "Closed Won", count: 30, pipelinevalue: 11725971},
 {Geo: "US West", SalesStage: "Closed Won", count: 7, pipelinevalue: 937536}] 
 
 group3=[{Geo: "US West", SalesStage: "Closed Lost", count: 91, pipelinevalue: 9799084},
 {Geo: "US East", SalesStage: "Deferred Opportunity", count: 26, pipelinevalue: 1858042},
 {Geo: "US West", SalesStage: "Deferred Opportunity", count: 3, pipelinevalue: 777000}]
 
 group4=[{Geo: "US West", SalesStage: "Identified", count: 24, pipelinevalue: 3140000},
 {Geo: "US East", SalesStage: "Identified", count: 22, pipelinevalue: 1303344}]

Ответы [ 2 ]

1 голос
/ 14 июля 2020

Вы можете использовать .filter() вот так:

let data = [{Geo: "US West", SalesStage: "Identified", count: 24, pipelinevalue: 3140000},
 {Geo: "US East", SalesStage: "Identified", count: 22, pipelinevalue: 1303344},
 {Geo: "NSU", SalesStage: "Proposal Submitted", count: 2, pipelinevalue: 554000},
 {Geo: "US East", SalesStage: "Proposal Submitted", count: 12, pipelinevalue: 4516510},
 {Geo: "NSU", SalesStage: "Qualified", count: 1, pipelinevalue: 60000},
 {Geo: "US West", SalesStage: "Proposal Submitted", count: 5, pipelinevalue: 977914},
 {Geo: "US East", SalesStage: "Qualified", count: 1, pipelinevalue: 480000},
 {Geo: "US West", SalesStage: "Qualified", count: 5, pipelinevalue: 1084400},
 {Geo: "US East", SalesStage: "Closed Won", count: 30, pipelinevalue: 11725971},
 {Geo: "US West", SalesStage: "Closed Won", count: 7, pipelinevalue: 937536},
 {Geo: "US West", SalesStage: "Closed Lost", count: 91, pipelinevalue: 9799084},
 {Geo: "US East", SalesStage: "Deferred Opportunity", count: 26, pipelinevalue: 1858042},
 {Geo: "US West", SalesStage: "Deferred Opportunity", count: 3, pipelinevalue: 777000}]
 
 let group1 = data.filter(obj => ['Qualified' ,'Proposal Submitted','Negotiation & Contracting'].includes(obj.SalesStage))
 console.log('group1')
 console.log(group1)
 
  let group2 = data.filter(obj => obj.SalesStage == 'Closed Won')
  console.log('group2')
 console.log(group2)
 
  let group3 = data.filter(obj => ['Closed Lost','Deferred Opportunity'].includes(obj.SalesStage))
  console.log('group3')
 console.log(group3)
 
  let group4 = data.filter(obj => ['Identified','QO under Evaluation'].includes(obj.SalesStage))
  console.log('group4')
 console.log(group4)
0 голосов
/ 14 июля 2020

Вы можете перебирать массив объектов и sh группу в соответствующую группу на основе условия.

let groups = [{Geo: "US West", SalesStage: "Identified", count: 24, pipelinevalue: 3140000},
 {Geo: "US East", SalesStage: "Identified", count: 22, pipelinevalue: 1303344},
 {Geo: "NSU", SalesStage: "Proposal Submitted", count: 2, pipelinevalue: 554000},
 {Geo: "US East", SalesStage: "Proposal Submitted", count: 12, pipelinevalue: 4516510},
 {Geo: "NSU", SalesStage: "Qualified", count: 1, pipelinevalue: 60000},
 {Geo: "US West", SalesStage: "Proposal Submitted", count: 5, pipelinevalue: 977914},
 {Geo: "US East", SalesStage: "Qualified", count: 1, pipelinevalue: 480000},
 {Geo: "US West", SalesStage: "Qualified", count: 5, pipelinevalue: 1084400},
 {Geo: "US East", SalesStage: "Closed Won", count: 30, pipelinevalue: 11725971},
 {Geo: "US West", SalesStage: "Closed Won", count: 7, pipelinevalue: 937536},
 {Geo: "US West", SalesStage: "Closed Lost", count: 91, pipelinevalue: 9799084},
 {Geo: "US East", SalesStage: "Deferred Opportunity", count: 26, pipelinevalue: 1858042},
 {Geo: "US West", SalesStage: "Deferred Opportunity", count: 3, pipelinevalue: 777000}];

  let group1 = [];
  let group2 = [];
  let group3 = [];
  let group4 = [];

  groups.forEach(group => {
    if (['Qualified', 'Proposal Submitted', 'Negotiation', 'Contracting'].includes(group.SalesStage)) {
      group1.push(group);
    } else if (group.SalesStage === 'Closed Won') {
      group2.push(group);
    } else if (['Closed Lost', 'Deferred Opportunity'].includes(group.SalesStage)) {
      group3.push(group);
    } else if (['Identified', 'QO under Evaluation'].includes(group.SalesStage)) {
      group4.push(group)
    }});

  console.log(group1);
  console.log(group2);
  console.log(group3);
  console.log(group4);
...