3

I am getting data from three different queries via Api. I want data to be merged without the duplicate data.

This is my function where i am merging the data:

getStaffCount(data) {
    if (data == null || data.results === null )
        return [];
    else
        return data.results.StaffCount.map(m => ({ Name: m.Name, Accounts: m.Accounts  })).
                    concat(data.results.RepProviderAccount.map(m => ({ Name: m.Name, Accnt: m.Accnt  }))).
                    concat( data.results.ProviderAccount.map(m => ({ Name: m.Name, Account: m.Account })));
}

This is my table:

<PowerTable Data={{ rows: this.getStaffCount(this.props.GridData) }}  rowsPerPage={5} orderBy="Name" order="asc" >
                        <PowerColumn id='Name' columnName='Name' numeric={false} disablePadding={false} label='Profile Name' width={100}>
                        </PowerColumn>
                        <PowerColumn id='Accounts' columnName='Accounts' numeric={false} disablePadding={false} label='Staff Accounts' width={100}>
                        </PowerColumn>
                        <PowerColumn id='Account' columnName='Account' numeric={false} disablePadding={false} label='Provider Account' width={100} >
                        </PowerColumn>
                        <PowerColumn id='Accnt' columnName='Accnt' numeric={false} disablePadding={false} label='Rep Provider Account' width={100} >
                        </PowerColumn>
                    </PowerTable>

This is the output i am getting right now

So in the above image same Profile Name(CNX MSL Platform) is coming twice. So is there any way i can merged those rows?

I want the Output in this way:
 Profile Name                     Staff      Provider      Rep Provider
 Cnx MSl Platform                   2                           1            
 Cnx Specilaity sales Platform      7                           22

Data: This is my Json Data

Manmeet
  • 39
  • 3
  • 5
    Would be easier to see how to merge if you just showed the json data. – Joe Lloyd Mar 12 '20 at 10:30
  • You only map name and account where does Staff, RepProvider and Provider come from? And how do you want to merge them, are they merged by name or account? – HMR Mar 12 '20 at 10:40
  • @HMR these are the column label names. name and account are the actual column names in the database – Manmeet Mar 12 '20 at 10:45
  • @JoeLloyd i am getting data from the SoQL query. Its an object but the spread operator seems to be not working – Manmeet Mar 12 '20 at 10:47

2 Answers2

0

As an object

if the data is an object the easy way to do that is the spread opperator

const combinedData = {
  ...dataSrc1,
  ...dataSrc2,
  ...dataSrc3,
}

All matching keys will be overwritten by the previous

As an array

It's a bit more complex. Assuming your object has a unique id (or any value to identify 2 as the same item) you can use a Set since they can only have unique values.

const array = [
  ...dataSrc1,
  ...dataSrc2,
  ...dataSrc3,
]
const unique = [...new Set(array.map(item => item.id))];
Joe Lloyd
  • 19,471
  • 7
  • 53
  • 81
0

Your answer to my question about what the data looks like and how to group them didn't make any sense, neither did you answer Joe just showed the json data and tell him where the data comes from instead of what it is.

So I assume you group by Name and Account is ignored. You can group them in the following way:

const data = {
  results: {
    StaffCount: [
      {
        Name: 'a',
        Accounts: 2,
      },
      {
        Name: 'b',
        Accounts: 20,
      },
    ],
    RepProviderAccount: [
      {
        Name: 'a',
        Accnt: 3,
      },
    ],
    ProviderAccount: [
      {
        Name: 'a',
        Account: 1,
      },
    ],
  },
};
const grouped = [
  ...data.results.StaffCount,
  ...data.results.RepProviderAccount,
  ...data.results.ProviderAccount,
].reduce((result, item) => {
  const {
    Name,
    Account = 0,
    Accounts = 0,
    Accnt = 0,
  } = item;
  const existing = result.get(item.Name) || {
    Name,
    Account: 0,
    Accounts: 0,
    Accnt: 0,
  };
  existing.Account += Account;
  existing.Accounts += Accounts;
  existing.Accnt += Accnt;
  return result.set(Name, existing);
}, new Map());
console.log([...grouped.values()]);

In case this doesn't work for you can you please update your question and provide code as in my answer with the expected input and output? You can respond to this answer and I'll have a look at your question again.

This may actually be an xy problem, you are fetching 3 data sources and then trying to group and sum them but maybe you can just get 1 data source and try salesforce to group and sum them in the query. I don't know enough about salesforce but maybe you can ask another question tagging it with soql if it's possible to just get the data grouped and summed.

HMR
  • 37,593
  • 24
  • 91
  • 160
  • I have updated the question if u need more info let me know – Manmeet Mar 12 '20 at 12:47
  • @Manmeet I will try to sum up your requirements by reverse engineering the puzzled description you are giving to it: You have 3 data sets containing an array of objects, all have a Name propety. StaffCount has an Accounts property, RepProviderAccount has an Accnt property and ProviderAccount has an Account property these properties are numbers. You want to sum the number properties grouped by name – HMR Mar 12 '20 at 13:07