下划线/破折号:按键的子字符串分组对象

时间:2019-05-13 12:29:09

标签: javascript underscore.js lodash

我正在尝试根据对象键的子字符串将对象分组为更有条理的结构。

下面是我要传递的对象的示例:

var data = {
  "summ_example_1": {
    "value": 40464.284237
  },
  "summ_example_number_2": {
    "value": 39260.605837
  },
  "in_process_example_1": {
    "count": 10,
    "value": 10
  },
  "in_process_example_2": {
    "count": 12,
    "value": 12.5
  },
  "in_process_example_3": {
    "count": 0,
    "value": 0
  },
  "awaiting_example_1": {
    "count": 17,
  },
  "awaiting_example_2": {
    "count": 51,
  }
}

我想要的输出是:

{ summ:
    ["summ_example_1": {"value": 40464.284237},
     "summ_example_number_2": {"value": 39260.605837}],
  in:
    ["in_process_example_1": {"count": 10, "value": 10},
     "in_process_example_2": {"count": 12, "value": 12.5},
     "in_process_example_3": {"count": 0, "value": 0}],
  awaiting:
    ["awaiting_example_1": {"count": 17},
     "awaiting_example_2": {"count": 51"}]}

我试图在Lodash和Underscore.js中使用groupBy对对象进行分组,但是没有任何运气。

在下划线下,我可以使用以下代码(请注意,这在Lodash中不起作用,因为我似乎无法获取对象密钥的值:

var grouped = _.groupBy(data, function(val, key){
    return key.substr(0, key.indexOf('_'));
});

产生以下输出:

{ summ:
    [{"value": 40464.284237},
     {"value": 39260.605837}],
  in:
    [{"count": 10, "value": 10},
     {"count": 12, "value": 12.5},
     {"count": 0, "value": 0}],
  awaiting:
    [{"count": 17},
     {"count": 51"}]}

但是我想将对象关键字保留在输出中,并且您可以看到它正在删除它们。

我正在做什么?

很抱歉,如果这没有道理或已被问到,但是我一直在寻找解决方案的时间超过了我的期望,并且开始耗尽创意。

感谢您抽出宝贵的时间阅读本文,请随时提出任何问题!

2 个答案:

答案 0 :(得分:4)

您可以获取对象的条目并构建新的密钥并以此密钥进行分组。

var data = { summ_example_1: { value: 40464.284237 }, summ_example_number_2: { value: 39260.605837 }, in_process_example_1: { count: 10, value: 10 }, in_process_example_2: { count: 12, value: 12.5 }, in_process_example_3: { count: 0, value: 0 }, awaiting_example_1: { count: 17 }, awaiting_example_2: { count: 51 } },
    result = Object.entries(data).reduce((r, [k, v]) => {
        var key = k.replace(/_.*$/, '') ;
        (r[key] = r[key] || {})[k] = v;
        return r;
    }, {});

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

答案 1 :(得分:0)

Lodash的_.groupBy()仅用值调用iteratee,此外,groupBy的结果是数组的对象,而不是所需对象的对象。

解决方案1-lodash:

您可以使用_.flow()创建一个函数,该函数将每个对象映射为包括组密钥(例如summ),并将它们合并在一起:

const { flow, map, partialRight: pr, spread, merge } = _

const fn = flow(
  pr(map, (v, k) => ({ [k.split('_')[0]]: { [k]: v }})), // format each object to have the group key - summ for example
  spread(merge) // merge all objects together
)

const data = { summ_example_1: { value: 40464.284237 }, summ_example_number_2: { value: 39260.605837 }, in_process_example_1: { count: 10, value: 10 }, in_process_example_2: { count: 12, value: 12.5 }, in_process_example_3: { count: 0, value: 0 }, awaiting_example_1: { count: 17 }, awaiting_example_2: { count: 51 } };

const result = fn(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

解决方案2-lodash / fp

由于lodash / fp _.map()不会将密钥传递给iteratee,因此请使用_.pairs()创建一个[key,value]元组。映射元组以包括组键,然后将它们全部合并在一起。

const { flow, toPairs, map, mergeAll } = _

const fn = flow(
  toPairs,
  map(([k, v]) => ({ [k.split('_')[0]]: { [k]: v }})),
  mergeAll
)

const data = { summ_example_1: { value: 40464.284237 }, summ_example_number_2: { value: 39260.605837 }, in_process_example_1: { count: 10, value: 10 }, in_process_example_2: { count: 12, value: 12.5 }, in_process_example_3: { count: 0, value: 0 }, awaiting_example_1: { count: 17 }, awaiting_example_2: { count: 51 } };

const result = fn(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>