如何通过一些键求和数组中的对象值?

时间:2018-12-16 16:53:29

标签: javascript lodash

我找不到解决此问题的方法。

我有数组:

const taxItems = [{
    taxCode: '430',
    taxAmount: 2,
    ItemTaxPercentage: 20,
    taxCategory: '2'
  },
  {
    taxCode: '430',
    taxAmount: 4,
    ItemTaxPercentage: 20,
    taxCategory: '2'
  },
  {
    taxCode: '431',
    taxAmount: 5,
    ItemTaxPercentage: 20,
    taxCategory: '2'
  },
  {
    taxCode: '430',
    taxAmount: 6,
    ItemTaxPercentage: 20,
    taxCategory: '2'
  }
]

然后从这个数组中我想按taxCode汇总所有对象。我想要这样的结果:

var results = [{
    taxCode: '430',
    taxAmount: 12,
    ItemTaxPercentage: 20,
    taxCategory: '2'
  },
  {
    taxCode: '431',
    taxAmount: 5,
    ItemTaxPercentage: 20,
    taxCategory: '2'
  }
]

我尝试使用lodash:

var results =
lodash(taxItems)
  .groupBy('taxCode')
  .map((objs, key) => ({
      'taxCode': key,
      'taxAmount': lodash.sumBy(objs, 'taxAmount') }))
  .value();

但是它仅适用于键值对。我将松开对象的原始结构。

什么是解决我的问题的正确方法?

4 个答案:

答案 0 :(得分:1)

将每个组的第一个对象散布到结果对象中,以获得重复出现的属性:

const taxItems = [{"taxCode":"430","taxAmount":2,"ItemTaxPercentage":20,"taxCategory":"2"},{"taxCode":"430","taxAmount":4,"ItemTaxPercentage":20,"taxCategory":"2"},{"taxCode":"431","taxAmount":5,"ItemTaxPercentage":20,"taxCategory":"2"},{"taxCode":"430","taxAmount":6,"ItemTaxPercentage":20,"taxCategory":"2"}];

const results = _(taxItems)
  .groupBy('taxCode')
  .map((objs, taxCode) => ({
      ..._.head(objs),
      taxCode,
      taxAmount: _.sumBy(objs, 'taxAmount') }))
  .value();
  
console.log(results);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

答案 1 :(得分:0)

这将起作用。简单的减少。您无需为此使用import altair as alt import pandas as pd index = pd.date_range('2018-01-01', '2018-01-31', freq='B') df = pd.DataFrame(pd.np.arange(len(index)), index=index, columns=['Y']) alt.Chart(df.reset_index()).mark_line().encode( x='index', y='Y' )

lodash

答案 2 :(得分:0)

使用简单的$data['orders'][] = array( 'order_id' => $order_id, 'invoice_no' => $invoice_no, 'date_added' => date($this->language->get('date_format_short'), strtotime($order_info['date_added'])), 'store_name' => $order_info['store_name'], 'store_url' => rtrim($order_info['store_url'], '/'), 'store_address' => nl2br($store_address), 'store_email' => $store_email, 'store_telephone' => $store_telephone, 'email' => $order_info['email'], 'telephone' => $order_info['telephone'], 'shipping_address' => $shipping_address, 'shipping_method' => $order_info['shipping_method'], 'product' => $product_data, 'comment' => nl2br($order_info['comment']), 'shipping_postcode' => $order_info['shipping_postcode'] ); ,您就可以这样做。

{{ order.shipping_postcode }}

答案 3 :(得分:0)

您还可以通过_.map_.groupBy_.mergeWith解决此问题:

const taxItems = [{ taxCode: '430', taxAmount: 2, ItemTaxPercentage: 20, taxCategory: '2' }, { taxCode: '430', taxAmount: 4, ItemTaxPercentage: 20, taxCategory: '2' }, { taxCode: '431', taxAmount: 5, ItemTaxPercentage: 20, taxCategory: '2' }, { taxCode: '430', taxAmount: 6, ItemTaxPercentage: 20, taxCategory: '2' } ]

const result = _.map(_.groupBy(taxItems, 'taxCode'), x => 
  _.mergeWith(...x, (o,s,k) => k == 'taxAmount' ? o+s : s))

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>