【问题标题】:Sum the number properties grouped by name对按名称分组的数字属性求和
【发布时间】:2020-06-26 15:56:23
【问题描述】:
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>

如果它们具有相同的配置文件名称,我想添加列值。(我通过 Api 从 SoQL 查询中获取数据) 这是我的数据:

这是我对它们进行分组和唯一化的功能,但这似乎不起作用:

groupBy(column, data) {
    var groups = {};

    data.forEach(function (itm) {
      groups[itm[column]] = groups[itm[column]] || [];
      groups[itm[column]].push(itm);
    });

    return groups;
  }

   uniquify(groups) {
    var unique = [];
    for (var key in groups) {
      if (groups.hasOwnProperty(key)) {
        unique.push(groups[key][0]);
      }
    }

    return unique;
  }
   arrUnique(arr) {
    var groupsByName = this.groupBy("Name", arr);
    var uniqueNames = this.uniquify(groupsByName);
   return uniqueNames;
  }

【问题讨论】:

    标签: javascript node.js reactjs merge soql


    【解决方案1】:

    这将最适合减少。

    const results = data.reduce((arr, dat) => {
      // Find the index of the item with this name...
      let index = arr.findIndex(item => item.name === dat.name)
    
      // If it's not -1 this will be the index where this item has
      // already been added. Otherwise it doesn't exist yet
      // so push it...
      if(index !== -1) arr[index].account += dat.account
      else arr.push(dat)
     }, [])
    

    类似的东西应该给你一个对象数组,其中所有account 属性都相乘在一起。

    注意我注意到您有时有“帐户”,有时有“帐户”,您必须编写一些额外的代码来弥补这一点。所以这只是一个大概的大纲。

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多