合并和数量添加javascript对象

时间:2016-05-10 14:02:18

标签: javascript arrays coffeescript lodash

您好我有以下内容:

> 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' } ]
> 

这是错误的,因为我得到两个{ sku: 'EP01', qty: 100, cntry: 'DE' }因为我希望获得一个合并对象,如{ sku: 'EP01', total: 110, qty-gb: 10, qty-de: 100 }

我的列表有大约8,000个条目,在某些情况下,一个可能存在于一个而不是另一个。

使用lodash在node.js上实现此目的的有效方法是什么?

我试过了:

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

但不正确

3 个答案:

答案 0 :(得分:1)

注意:我不知道如何将下面的代码更改为coffreescript,所以我只是在javascript中回答这个问题。

您可以执行以下操作:

  1. 使用groupBy()sku分组项目。
  2. map()已分组的项目,并按国家/地区reduce()返回已转换的qty个密钥,并通过sumBy()返回total值。
  3. &#13;
    &#13;
    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>');
    &#13;
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.js"></script>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

普通Javascript中的解决方案,其中一个对象作为对插入对象元素的引用。

var gb = [{ "sku": "EP01", "qty": 10, "cntry": "GB" }, { "sku": "EP02", "qty": 5, "cntry": "GB" }, { "sku": "EP03", "qty": 15, "cntry": "GB" }],
    de = [{ "sku": "EP01", "qty": 100, "cntry": "DE" }, { "sku": "EP02", "qty": 25, "cntry": "DE" }],
    result = function (data) {
        function update(a) {
            if (!hash[a.sku]) {
                hash[a.sku] = { sku: a.sku, total: 0 };
                r.push(hash[a.sku]);
            }
            hash[a.sku]['qty-' + a.cntry.toLowerCase()] = a.qty;
            hash[a.sku].total += a.qty;
        }

        var hash = Object.create(null),
            r = [];

        data.forEach(a => a.forEach(update));
        return r;
    }([gb, de]);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

答案 2 :(得分:0)

由于您在Node上,所有ES6好东西都在为您服务。所以我不会使用额外的库,如lodash或下划线。你可能会喜欢这个

&#13;
&#13;
var gb = [{"sku": "EP01", "qty": 10, "cntry": "GB"}, {"sku": "EP02", "qty": 5, "cntry": "GB"}, {"sku": "EP03", "qty": 15, "cntry": "GB"}],
    de = [{"sku": "EP01", "qty": 100, "cntry": "DE"}, {"sku": "EP02", "qty": 25, "cntry": "DE"}];
merged = (...arrs) => arrs.reduce((p,c) => c.reduce((f,s) => f.concat(s),p),[]);
document.write("<pre>" + JSON.stringify(merged(gb,de),null,2) + "</pre>");
&#13;
&#13;
&#13;

如果您怀疑可能发生任何重复,最好在链的末尾应用过滤器。我也将实施那个fyi。