在使用 Lo-Dash 3.0 转换值时分组的任何好方法
Any nice way to groupBy while transforming values with Lo-Dash 3.0?
给定一个这样的对象数组:
var data = [
{key: 'a', val: '1'},
{key: 'a', val: '2'},
{key: 'b', val: '3'},
{key: 'c', val: '4'},
{key: 'c', val: '5'},
{key: 'c', val: '6'}
];
我想将其转换为:
var desiredResults = {
'a': [1, 2],
'b': [3],
'c': [4, 5, 6]
};
到目前为止,我已经找到了两种方法来实现这一点 lodash-fp
,但我仍然想知道是否有更好的方法。
第一种方式有点程序化:
var out = _(data)
.transform(function(out, item) {
out[item.key] = out[item.key] || [];
out[item.key].push(item.val);
}, {});
第二种方式是我希望实现的无点风格:
var out = _(data)
.groupBy(_.property('key'))
.mapValues(_.map(_.property('val')))
.value();
// Yes, I know that _.property is implied if I just pass a string
但是,这比我想要的更混乱:我必须迭代中间结果以转换分组值,我认为它掩盖了代码试图实现的目标。 不过,我不能先变身,因为我想要的变身会删除键!
有没有像groupByTransforming(groupIteratee, transformIteratee)
方法这样的东西?
我对 LoDash 一无所知(抱歉),但我有一个简单的函数,可以做你想做的事,只使用香草 JS:
/**
* Maps an array of objects into a single object,
* grouped by one property and supplying another.
* @param {Array} input The array of objects containg data
* @param {String} groupBy The name of the property to group the elements by
* @param {String} groupProp The property to push into each array
* @return {Object} The mapped object.
*/
function mapToObject(input, groupBy, groupProp) {
var obj = {};
// Loop through the data
input.forEach(function (data) {
// If the output object doesn't contain the key,
// make it as an empty array
if (!obj[data[groupBy]]) {
obj[data[groupBy]] = [];
};
// Push the value into the obj[groupBy] array
obj[data[groupBy]].push(data[groupProp]);
});
return obj;
}
在您的情况下,您将像这样使用它:
mapToObject(data, 'key', 'val'),所以它会返回一个按'key'分组的数据对象,值为'val'。有关示例,请参阅下面的代码段:
var data = [
{key: 'a', val: '1'},
{key: 'a', val: '2'},
{key: 'b', val: '3'},
{key: 'c', val: '4'},
{key: 'c', val: '5'},
{key: 'c', val: '6'}
];
/**
* Maps an array of objects into a single object,
* grouped by one property and supplying another.
* @param {Array} input The array of objects containg data
* @param {String} groupBy The name of the property to group the elements by
* @param {String} groupProp The property to push into each array
* @return {Object} The mapped object.
*/
function mapToObject(input, groupBy, groupProp) {
var obj = {};
// Loop through the data
input.forEach(function (data) {
// If the output object doesn't contain the key,
// make it as an empty array
if (!obj[data[groupBy]]) {
obj[data[groupBy]] = [];
};
// Push the value into the obj[groupBy] array
obj[data[groupBy]].push(data[groupProp]);
});
return obj;
}
// Just to show example output:
document.write(JSON.stringify(mapToObject(data, 'key', 'val')));
相关文章:
- 在主干视图中调用render()方法的好方法
- 任何好的 JavaScript Eclipse 插件
- AngularJS:有没有比指定的方法更好的方法来实现这一点
- 优化javascript的任何更好的方法
- 在使用 Lo-Dash 3.0 转换值时分组的任何好方法
- 是否有任何好的下划线.js替代方案来扩展核心 js 类
- 组合多个回调的任何更好方法
- 克罗克福德的去实体化方法 - 《好部分》第41页
- 将值从第1页传递到第3页的任何更好的方法
- 哪种验证方法更好
- 任何好的javascript库都可以允许对网站进行爬网
- Javascript文件结构-任何好的解决方案
- 任何更新网页一部分的好方法
- 任何好的JavaScript动画框架
- 在没有任何公共属性的情况下垂直连接数组的更好方法
- 是否有任何好的客户端股票图表库
- 为Web应用程序仅组合必要的css和js的任何好方法
- 编写构造器的扩展方法的好方法?(请参阅我的尝试)
- 哪一种定义变量的方法更好?为什么?
- 是否有任何好的或可靠的方法可以仅使用Internet Explorer错误消息来确定JavaScript错误的位置