按对象数组分组

时间:2019-09-05 11:11:41

标签: javascript json group-by

我正在根据以下第一项的值按对象数组分组:

const json2 = [{"value":"value1","metric":1},{"value":"value1","metric":2},{"value":"value3","metric":0},{"value":"value2","metric":4},{"value":"value3","metric":1},{"value":"value3","metric":1}];

const result2 = [...json2.reduce((r, o) => {
  const key = o.value;
  
  const item = r.get(key) || Object.assign({}, o, {
    metric: 0,
  });
  
  item.metric += o.metric;

  return r.set(key, item);
}, new Map).values()];

console.log(result2);

但是当我有如下所示的对象数组时,我正在努力做同样的事情:

[[{"value":"value1","formattedValue":"value1"},{"value":2831.8,"formattedValue":"283,180.00 %"}],[{"value":"value1","formattedValue":"value1"},{"value":349.1111111111111,"formattedValue":"34,911.11 %"}],[{"value":"value2","formattedValue":"value2"},{"value":3.3703703703703702,"formattedValue":"337.04 %"}]]

因此,与以前的JSON相比,不同之处在于:

{"value":"value1","metric":1}

现在是:

[{"value":"value1","formattedValue":"value1"},{"value":"1.0","formattedValue":100.00 %}]

一种解决方案是操纵第二个查询,使其看起来像第一个查询,但在这种情况下,我想使其与上面的函数一起使用。

想要的结果是:

[[{"value":"value1","formattedValue":"value1"},{"value":3180.91111111,"formattedValue":"318091.11 %"}],[{"value":"value2","formattedValue":"value2"},{"value":3.3703703703703702,"formattedValue":"337.04 %"}]]

2 个答案:

答案 0 :(得分:3)

您可以通过解构获取键/值并更新结果集。

var data = [[{ value: "value1", formattedValue: "value1" }, { value: 2831.8, formattedValue: "283,180.00 %" }], [{ value: "value1", formattedValue: "value1" }, { value: 349.1111111111111, formattedValue: "34,911.11 %" }], [{ value: "value2", formattedValue: "value2" }, { value: 3.3703703703703702, formattedValue: "337.04 %" }]],
    result = Array.from(
        data
            .reduce((m, a) => {
                var [{ value: key }, { value }] = a,
                    target = m.get(key);

                if (!target) return m.set(key, JSON.parse(JSON.stringify(a)));
                target[1].value += value;
                target[1].formattedValue = `${(target[1].value * 100).toFixed(2) } %`;
                return m;
                
            }, new Map)
            .values()                
    );        

   console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:-1)

  I think your are trying to flatten the arrays inside of an array. you can do something like this.

var arrays=[[{"value":"value1","formattedValue":"value1"},{"value":2831.8,"formattedValue":"283,180.00 %"}],[{"value":"value1","formattedValue":"value1"},{"value":349.1111111111111,"formattedValue":"34,911.11 %"}],[{"value":"value2","formattedValue":"value2"},{"value":3.3703703703703702,"formattedValue":"337.04 %"}]];

 var result =arrays.reduce(function(a, b){
         return a.concat(b);
 }, []);

 console.log(result);

然后您可以像这样使用现有功能。

const result2 = [...result.reduce((r, o) => {
  const key = o.value;

  const item = r.get(key) || Object.assign({}, o, {
    metric: 0,
  });

  item.metric += o.metric;

  return r.set(key, item);
}, new Map).values()];

console.log(result2);