【问题标题】:Merge Data from different Queries without duplicates合并来自不同查询的数据而不重复
【发布时间】:2020-06-24 09:16:04
【问题描述】:

我通过 Api 从三个不同的查询中获取数据。我希望在没有重复数据的情况下合并数据。

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

这是我的桌子:

<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

数据:

【问题讨论】:

  • 如果你只显示json数据会更容易看到如何合并。
  • 您只映射名称和帐户,Staff、RepProvider 和 Provider 来自哪里?以及您想如何合并它们,它们是按名称还是帐户合并?
  • @HMR 这些是列标签名称。 name 和 account 是数据库中的实际列名
  • @JoeLloyd 我正在从 SoQL 查询中获取数据。它是一个对象,但扩展运算符似乎不起作用

标签: javascript reactjs merge soql


【解决方案1】:

你对我关于数据是什么样子以及如何对它们进行分组的问题的回答没有任何意义,你也没有回答 Joe just showed the json data 并告诉他哪里数据来自它是什么。

所以我假设您按名称分组并且帐户被忽略。您可以按以下方式对它们进行分组:

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

如果这对您不起作用,您能否更新您的问题并提供我的答案中的代码以及预期的输入和输出?您可以回复此答案,我会再次查看您的问题。

这实际上可能是一个 xy 问题,您正在获取 3 个数据源,然后尝试对它们进行分组和求和,但也许您可以只获得 1 个数据源并尝试 salesforce 在查询中对它们进行分组和求和。我对 salesforce 的了解不够,但如果可以将数据分组和汇总,也许你可以问另一个问题,用 soql 标记它。

【讨论】:

  • 我已经更新了问题,如果您需要更多信息,请告诉我
  • @Manmeet 我将尝试通过逆向工程来总结您的要求,您给它的困惑描述:您有 3 个包含对象数组的数据集,都有一个 Name 属性。 StaffCount 有一个 Accounts 属性,RepProviderAccount 有一个 Accnt 属性,ProviderAccount 有一个 Account 属性,这些属性是数字。您想对按名称分组的数字属性求和
【解决方案2】:

作为一个对象

如果数据是一个对象,那么简单的方法就是传播运算符

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

所有匹配的键将被前一个覆盖

作为数组

这有点复杂。假设您的对象具有唯一 id(或任何将 2 标识为同一项目的值),您可以使用 Set,因为它们只能具有唯一值。

const array = [
  ...dataSrc1,
  ...dataSrc2,
  ...dataSrc3,
]
const unique = [...new Set(array.map(item => item.id))];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多