khinester khinester - 6 months ago 18
Javascript Question

merge and quantity add javascript objects

Hello I have the following:

> var gb = [{"sku": "EP01", "qty": 10, "cntry": "GB"}, {"sku": "EP02", "qty": 5, "cntry": "GB"}, {"sku": "EP03", "qty": 15, "cntry": "GB"}];
>
> var de = [{"sku": "EP01", "qty": 100, "cntry": "DE"}, {"sku": "EP02", "qty": 25, "cntry": "DE"}];
> var obj1 = gb.concat(de);
[ { sku: 'EP01', qty: 100, cntry: 'DE' },
{ sku: 'EP02', qty: 25, cntry: 'DE' },
{ sku: 'EP03', qty: 15, cntry: 'GB' },
{ sku: 'EP01', qty: 100, cntry: 'DE' },
{ sku: 'EP02', qty: 25, cntry: 'DE' } ]
>


This is wrong as I get two
{ sku: 'EP01', qty: 100, cntry: 'DE' }
where as I would like to get one merged object like
{ sku: 'EP01', total: 110, qty-gb: 10, qty-de: 100 }


My list has about 8,000 entries and in some cases one may exist in one but not the other.

What is an efficient way to achieve this on node.js with lodash?

I tried this:

allStockAPI = exports.allStockAPI = (req) ->
Promise.props
stocksAPI: stockAPI(req)
germanStocks: germanStocks.getStocks(config.germanStocksUrl)
.then (result) ->
newDe = result.germanStocks.map (i) ->
return Object.values(i)
result.stocksAPI.forEach (i) ->
i.pop()

rows = result.stocksAPI.concat newDe
#console.log rows
skus = rows.map (row) ->
{
sku: row[0],
val: row
}
groupedRows = _(skus).groupBy('sku').value()
rows = _(Object.keys(groupedRows)).uniq().value().map (sku) ->
rows = groupedRows[sku].map (row) ->
row.val
rows = rows.map (row) ->
if row[row.length - 2]
row[row.length - 2] = row[row.length - 1] + ' office : ' + row[row.length - 2]
if row[row.length - 2] == null
row[row.length - 2] = ''
row

stock = rows.reduce (prev, cur) ->
prev[prev.length - 1] = 'UK/DE'
if (prev[prev.length - 2])
prev[prev.length - 2] = prev[prev.length - 2] + '<br>' + cur[prev.length - 2]
else
prev[prev.length - 2] = cur[prev.length - 2]
prev
stock
rows


but is incorrect

Answer

Note: I don't know how to change the code below to coffreescript so I'll just answer this in javascript.

You can do the following:

  1. Use groupBy() to group items by sku.
  2. map() the grouped items and return the transformed qty keys by country through reduce() and the total value through sumBy().

var gb = [{"sku": "EP01", "qty": 10, "cntry": "GB"}, {"sku": "EP02", "qty": 5, "cntry": "GB"}, {"sku": "EP03", "qty": 15, "cntry": "GB"}];

var de = [{"sku": "EP01", "qty": 100, "cntry": "DE"}, {"sku": "EP02", "qty": 25, "cntry": "DE"}];

var result = _(gb)
  .concat(de)
  .groupBy('sku')
  .map(function(items) {
    return _.reduce(items, function(acc, item) {
      return _(item)
        .omit(['qty', 'cntry'])
        .set('qty-' + item.cntry, item.qty)
        .assign(acc)
        .value();
    }, { total: _.sumBy(items, 'qty') });
  })
  .value();

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.js"></script>

Comments