Underscore/Lodash:按键的子串对对象进行分组
Underscore/Lodash: Group object by substring of key
我正在尝试根据对象键的子字符串将对象分组为更有组织的结构。
下面是我传递的对象的示例:
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"}]}
但是我想将对象键保留在我的输出中,如您所见,它正在删除它们。
我正在做的事情可行吗?
如果这没有意义或已经被问到,我深表歉意,但我寻找解决方案的时间比我希望的要多,并且开始 运行 没有想法。
感谢您抽出宝贵时间阅读本文,如有任何问题,请随时提出!
您可以获取对象的条目并构建一个新键并按此键分组。
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; }
Lodash 的 _.groupBy()
只用值调用迭代器,此外 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()
不将键传递给被迭代对象,因此使用_.pairs()
创建一个[键,值] 元组。映射元组以包含组键,并将它们全部合并在一起。
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>
我正在尝试根据对象键的子字符串将对象分组为更有组织的结构。
下面是我传递的对象的示例:
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"}]}
但是我想将对象键保留在我的输出中,如您所见,它正在删除它们。
我正在做的事情可行吗?
如果这没有意义或已经被问到,我深表歉意,但我寻找解决方案的时间比我希望的要多,并且开始 运行 没有想法。
感谢您抽出宝贵时间阅读本文,如有任何问题,请随时提出!
您可以获取对象的条目并构建一个新键并按此键分组。
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; }
Lodash 的 _.groupBy()
只用值调用迭代器,此外 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()
不将键传递给被迭代对象,因此使用_.pairs()
创建一个[键,值] 元组。映射元组以包含组键,并将它们全部合并在一起。
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>