Merge Data from different Queries without duplicates

回眸只為那壹抹淺笑 提交于 2021-01-27 14:11:47

问题


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>

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:


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/60652109/merge-data-from-different-queries-without-duplicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!