在数组中对对象进行分组的最有效方法是什么?
例如,给定这个对象数组:
[
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
]
我在表格中显示这些信息。我想用不同的方法进行分组,但我想总结这些值。
我正在使用Underscore.js作为其groupby函数,这很有帮助,但并没有完成整个技巧,因为我不希望它们“拆分”但是“合并”,更像是SQL {{ 1}}方法。
我正在寻找的是能够总计特定值(如果需要)。
因此,如果我按group by
进行分组,我希望收到:
Phase
如果我分组[
{ Phase: "Phase 1", Value: 50 },
{ Phase: "Phase 2", Value: 130 }
]
/ Phase
,我会收到:
Step
是否有一个有用的脚本,或者我应该坚持使用Underscore.js,然后循环结果对象来自己完成总计?
答案 0 :(得分:512)
如果你想避免使用外部库,你可以简洁地实现groupBy()
的vanilla版本,如下所示:
var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
console.log(groupBy(['one', 'two', 'three'], 'length'));
// => {3: ["one", "two"], 5: ["three"]}
答案 1 :(得分:143)
使用ES6 Map对象:
function groupBy(list, keyGetter) {
const map = new Map();
list.forEach((item) => {
const key = keyGetter(item);
const collection = map.get(key);
if (!collection) {
map.set(key, [item]);
} else {
collection.push(item);
}
});
return map;
}
// example usage
const pets = [
{type:"Dog", name:"Spot"},
{type:"Cat", name:"Tiger"},
{type:"Dog", name:"Rover"},
{type:"Cat", name:"Leo"}
];
const grouped = groupBy(pets, pet => pet.type);
console.log(grouped.get("Dog")); // -> [{type:"Dog", name:"Spot"}, {type:"Dog", name:"Rover"}]
console.log(grouped.get("Cat")); // -> [{type:"Cat", name:"Tiger"}, {type:"Cat", name:"Leo"}]
关于地图: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
答案 2 :(得分:86)
使用ES6:
const groupBy = (items, key) => items.reduce(
(result, item) => ({
...result,
[item[key]]: [
...(result[item[key]] || []),
item,
],
}),
{},
);
答案 3 :(得分:53)
虽然linq答案很有趣,但它的重量也很重。我的方法有所不同:
var DataGrouper = (function() {
var has = function(obj, target) {
return _.any(obj, function(value) {
return _.isEqual(value, target);
});
};
var keys = function(data, names) {
return _.reduce(data, function(memo, item) {
var key = _.pick(item, names);
if (!has(memo, key)) {
memo.push(key);
}
return memo;
}, []);
};
var group = function(data, names) {
var stems = keys(data, names);
return _.map(stems, function(stem) {
return {
key: stem,
vals:_.map(_.where(data, stem), function(item) {
return _.omit(item, names);
})
};
});
};
group.register = function(name, converter) {
return group[name] = function(data, names) {
return _.map(group(data, names), converter);
};
};
return group;
}());
DataGrouper.register("sum", function(item) {
return _.extend({}, item.key, {Value: _.reduce(item.vals, function(memo, node) {
return memo + Number(node.Value);
}, 0)});
});
你可以看到它in action on JSBin。
我在Underscore中没有看到has
所做的任何事情,尽管我可能会错过它。它与_.contains
非常相似,但使用_.isEqual
而不是===
进行比较。除此之外,其余部分是特定于问题的,尽管试图是通用的。
现在DataGrouper.sum(data, ["Phase"])
返回
[
{Phase: "Phase 1", Value: 50},
{Phase: "Phase 2", Value: 130}
]
DataGrouper.sum(data, ["Phase", "Step"])
返回
[
{Phase: "Phase 1", Step: "Step 1", Value: 15},
{Phase: "Phase 1", Step: "Step 2", Value: 35},
{Phase: "Phase 2", Step: "Step 1", Value: 55},
{Phase: "Phase 2", Step: "Step 2", Value: 75}
]
但sum
只是这里的一个潜在功能。您可以随意注册其他人:
DataGrouper.register("max", function(item) {
return _.extend({}, item.key, {Max: _.reduce(item.vals, function(memo, node) {
return Math.max(memo, Number(node.Value));
}, Number.NEGATIVE_INFINITY)});
});
现在DataGrouper.max(data, ["Phase", "Step"])
将返回
[
{Phase: "Phase 1", Step: "Step 1", Max: 10},
{Phase: "Phase 1", Step: "Step 2", Max: 20},
{Phase: "Phase 2", Step: "Step 1", Max: 30},
{Phase: "Phase 2", Step: "Step 2", Max: 40}
]
或者如果你注册了这个:
DataGrouper.register("tasks", function(item) {
return _.extend({}, item.key, {Tasks: _.map(item.vals, function(item) {
return item.Task + " (" + item.Value + ")";
}).join(", ")});
});
然后调用DataGrouper.tasks(data, ["Phase", "Step"])
会让你
[
{Phase: "Phase 1", Step: "Step 1", Tasks: "Task 1 (5), Task 2 (10)"},
{Phase: "Phase 1", Step: "Step 2", Tasks: "Task 1 (15), Task 2 (20)"},
{Phase: "Phase 2", Step: "Step 1", Tasks: "Task 1 (25), Task 2 (30)"},
{Phase: "Phase 2", Step: "Step 2", Tasks: "Task 1 (35), Task 2 (40)"}
]
DataGrouper
本身就是一个功能。您可以使用您的数据和要分组的属性列表来调用它。它返回一个数组,其元素是具有两个属性的对象:key
是分组属性的集合,vals
是一个对象数组,包含不在键中的其余属性。例如,DataGrouper(data, ["Phase", "Step"])
将产生:
[
{
"key": {Phase: "Phase 1", Step: "Step 1"},
"vals": [
{Task: "Task 1", Value: "5"},
{Task: "Task 2", Value: "10"}
]
},
{
"key": {Phase: "Phase 1", Step: "Step 2"},
"vals": [
{Task: "Task 1", Value: "15"},
{Task: "Task 2", Value: "20"}
]
},
{
"key": {Phase: "Phase 2", Step: "Step 1"},
"vals": [
{Task: "Task 1", Value: "25"},
{Task: "Task 2", Value: "30"}
]
},
{
"key": {Phase: "Phase 2", Step: "Step 2"},
"vals": [
{Task: "Task 1", Value: "35"},
{Task: "Task 2", Value: "40"}
]
}
]
DataGrouper.register
接受一个函数并创建一个新函数,该函数接受初始数据和要分组的属性。然后,这个新函数采用上面的输出格式,依次对每个函数运行函数,返回一个新数组。生成的函数根据您提供的名称存储为DataGrouper
的属性,如果您只想要本地引用,也会返回。
那是很多解释。我希望代码相当简单!
答案 4 :(得分:38)
使用linq.js
可能更容易做到这一点,DEMO旨在真正实现JavaScript中的LINQ(DEMO):
var linq = Enumerable.From(data);
var result =
linq.GroupBy(function(x){ return x.Phase; })
.Select(function(x){
return {
Phase: x.Key(),
Value: x.Sum(function(y){ return y.Value|0; })
};
}).ToArray();
结果:
[
{ Phase: "Phase 1", Value: 50 },
{ Phase: "Phase 2", Value: 130 }
]
或者,更简单地使用基于字符串的选择器({{3}}):
linq.GroupBy("$.Phase", "",
"k,e => { Phase:k, Value:e.Sum('$.Value|0') }").ToArray();
答案 5 :(得分:37)
我会检查lodash groupBy它似乎完全符合您的要求。它也非常轻巧,非常简单。
小提琴示例:https://jsfiddle.net/r7szvt5k/
如果你的数组名称是:hover
,那么带有lodash的groupBy就是:
arr
答案 6 :(得分:19)
_.groupBy([{tipo: 'A' },{tipo: 'A'}, {tipo: 'B'}], 'tipo');
>> Object {A: Array[2], B: Array[1]}
答案 7 :(得分:16)
您可以从Map
构建ES6 array.reduce()
。
const groupedMap = initialArray.reduce(
(entryMap, e) => entryMap.set(e.id, [...entryMap.get(e.id)||[], e]),
new Map()
);
与其他解决方案相比,这有一些优势:
_.groupBy()
不同)Map
而不是对象(例如_.groupBy()
返回的对象)。这有lots of benefits,包括:
Map
是一个数组数组更有用的结果。但是如果你想要一个数组数组,那么你可以调用Array.from(groupedMap.entries())
(对于[key, group array]
对的数组)或Array.from(groupedMap.values())
(对于一个简单的数组数组)。作为最后一点的一个例子,假设我有一个对象数组,我想通过id进行(浅)合并,如下所示:
const objsToMerge = [{id: 1, name: "Steve"}, {id: 2, name: "Alice"}, {id: 1, age: 20}];
// The following variable should be created automatically
const mergedArray = [{id: 1, name: "Steve", age: 20}, {id: 2, name: "Alice"}]
为此,我通常首先按id分组,然后合并每个结果数组。相反,您可以直接在reduce()
:
const mergedArray = Array.from(
objsToMerge.reduce(
(entryMap, e) => entryMap.set(e.id, {...entryMap.get(e.id)||{}, ...e}),
new Map()
).values()
);
答案 8 :(得分:15)
Array.prototype.groupBy = function(keyFunction) {
var groups = {};
this.forEach(function(el) {
var key = keyFunction(el);
if (key in groups == false) {
groups[key] = [];
}
groups[key].push(el);
});
return Object.keys(groups).map(function(key) {
return {
key: key,
values: groups[key]
};
});
};
答案 9 :(得分:14)
您可以使用Alasql JavaScript库:
执行此操作var data = [ { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" }];
var res = alasql('SELECT Phase, Step, SUM(CAST([Value] AS INT)) AS [Value] \
FROM ? GROUP BY Phase, Step',[data]);
试试这个例子at jsFiddle。
BTW: 在大型阵列上(100000条记录以及更多)Alasql更快到了Linq。请参阅测试at jsPref。
评论:
答案 10 :(得分:8)
MDN在其Array.reduce()
文档中有this example。
// Grouping objects by a property
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Grouping_objects_by_a_property#Grouping_objects_by_a_property
var people = [
{ name: 'Alice', age: 21 },
{ name: 'Max', age: 20 },
{ name: 'Jane', age: 20 }
];
function groupBy(objectArray, property) {
return objectArray.reduce(function (acc, obj) {
var key = obj[property];
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(obj);
return acc;
}, {});
}
var groupedPeople = groupBy(people, 'age');
// groupedPeople is:
// {
// 20: [
// { name: 'Max', age: 20 },
// { name: 'Jane', age: 20 }
// ],
// 21: [{ name: 'Alice', age: 21 }]
// }
答案 11 :(得分:8)
虽然这个问题有一些答案,而且答案看起来有点过于复杂,但我建议使用vanilla Javascript进行分组(如果需要)Map
。
function groupBy(array, groups, valueKey) {
var map = new Map;
groups = [].concat(groups);
return array.reduce((r, o) => {
groups.reduce((m, k, i, { length }) => {
var child;
if (m.has(o[k])) return m.get(o[k]);
if (i + 1 === length) {
child = Object
.assign(...groups.map(k => ({ [k]: o[k] })), { [valueKey]: 0 });
r.push(child);
} else {
child = new Map;
}
m.set(o[k], child);
return child;
}, map)[valueKey] += +o[valueKey];
return r;
}, [])
};
var data = [{ Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" }, { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" }, { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" }, { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" }, { Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" }, { Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }];
console.log(groupBy(data, 'Phase', 'Value'));
console.log(groupBy(data, ['Phase', 'Step'], 'Value'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 12 :(得分:6)
没有突变:
$scope.insertData=function(){
$http.post("cgi-bin/in.cgi?bname=" + $scope.bname + "&bphone=" + $scope.bphone, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function(response){
console.log("Data Inserted Successfully");
},function(error){
alert("Sorry! Data Couldn't be inserted!");
console.error(error);
});
}
});
答案 13 :(得分:6)
此解决方案采用任意函数(不是键),因此它比上述解决方案更灵活,并允许arrow functions,类似于 LINQ 中使用的lambda expressions:
Array.prototype.groupBy = function (funcProp) {
return this.reduce(function (acc, val) {
(acc[funcProp(val)] = acc[funcProp(val)] || []).push(val);
return acc;
}, {});
};
注意:是否要扩展Array
的原型取决于您。
大多数浏览器支持的示例:
[{a:1,b:"b"},{a:1,c:"c"},{a:2,d:"d"}].groupBy(function(c){return c.a;})
使用箭头功能(ES6)的示例:
[{a:1,b:"b"},{a:1,c:"c"},{a:2,d:"d"}].groupBy(c=>c.a)
以上两个例子都返回:
{
"1": [{"a": 1, "b": "b"}, {"a": 1, "c": "c"}],
"2": [{"a": 2, "d": "d"}]
}
答案 14 :(得分:6)
我想建议我的做法。首先,单独分组和聚合。让我们宣布典型的“分组依据”功能。它需要另一个函数来为每个要分组的数组元素生成“哈希”字符串。
Array.prototype.groupBy = function(hash){
var _hash = hash ? hash : function(o){return o;};
var _map = {};
var put = function(map, key, value){
if (!map[_hash(key)]) {
map[_hash(key)] = {};
map[_hash(key)].group = [];
map[_hash(key)].key = key;
}
map[_hash(key)].group.push(value);
}
this.map(function(obj){
put(_map, obj, obj);
});
return Object.keys(_map).map(function(key){
return {key: _map[key].key, group: _map[key].group};
});
}
分组完成后,您可以根据需要汇总数据
data.groupBy(function(o){return JSON.stringify({a: o.Phase, b: o.Step});})
/* aggreagating */
.map(function(el){
var sum = el.group.reduce(
function(l,c){
return l + parseInt(c.Value);
},
0
);
el.key.Value = sum;
return el.key;
});
共同点起作用。我已经在chrome控制台中测试了这段代码。随时改进并发现错误;)
答案 15 :(得分:5)
const groupBy = (x,f)=>x.reduce((a,b)=>((a[f(b)]||=[]).push(b),a),{});
示例
const groupBy = (x, f) => x.reduce((a, b) => ((a[f(b)] ||= []).push(b), a), {});
// f -> should must return string/number because it will be use as key in object
// for demo
groupBy([1, 2, 3, 4, 5, 6, 7, 8, 9], v => (v % 2 ? "odd" : "even"));
// { odd: [1, 3, 5, 7, 9], even: [2, 4, 6, 8] };
const colors = [
"Apricot",
"Brown",
"Burgundy",
"Cerulean",
"Peach",
"Pear",
"Red",
];
groupBy(colors, v => v[0]); // group by colors name first letter
// {
// A: ["Apricot"],
// B: ["Brown", "Burgundy"],
// C: ["Cerulean"],
// P: ["Peach", "Pear"],
// R: ["Red"],
// };
groupBy(colors, v => v.length); // group by length of color names
// {
// 3: ["Red"],
// 4: ["Pear"],
// 5: ["Brown", "Peach"],
// 7: ["Apricot"],
// 8: ["Burgundy", "Cerulean"],
// }
const data = [
{ comment: "abc", forItem: 1, inModule: 1 },
{ comment: "pqr", forItem: 1, inModule: 1 },
{ comment: "klm", forItem: 1, inModule: 2 },
{ comment: "xyz", forItem: 1, inModule: 2 },
];
groupBy(data, v => v.inModule); // group by module
// {
// 1: [
// { comment: "abc", forItem: 1, inModule: 1 },
// { comment: "pqr", forItem: 1, inModule: 1 },
// ],
// 2: [
// { comment: "klm", forItem: 1, inModule: 2 },
// { comment: "xyz", forItem: 1, inModule: 2 },
// ],
// }
groupBy(data, x => x.forItem + "-" + x.inModule); // group by module with item
// {
// "1-1": [
// { comment: "abc", forItem: 1, inModule: 1 },
// { comment: "pqr", forItem: 1, inModule: 1 },
// ],
// "2-1": [
// { comment: "klm", forItem: 1, inModule: 2 },
// { comment: "xyz", forItem: 1, inModule: 2 },
// ],
// }
答案 16 :(得分:3)
基于之前的答案
const groupBy = (prop) => (xs) =>
xs.reduce((rv, x) =>
Object.assign(rv, {[x[prop]]: [...(rv[x[prop]] || []), x]}), {});
如果您的环境支持,使用对象扩展语法来查看它会更好。
const groupBy = (prop) => (xs) =>
xs.reduce((acc, x) => ({
...acc,
[ x[ prop ] ]: [...( acc[ x[ prop ] ] || []), x],
}), {});
这里,我们的reducer采用部分形成的返回值(从空对象开始),并返回一个由前一个返回值的展开成员组成的对象,以及一个新成员,其密钥是从当前计算的iteree在prop
的值,其值是该道具的所有值以及当前值的列表。
答案 17 :(得分:3)
想象一下你有这样的东西:
"{\"items\": []}"
这样做:
try
{
if (dgvData.Rows.Count > 0)
{
xCellApp.Application.Workbooks.Add(Type.Missing);
for (int i = 1; i < dgvData.Columns.Count + 1; i++)
{
xCellApp.Cells[1, i] = dgvData.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dgvData.Rows.Count - 1; i++)
{
for (int j = 0; j < dgvData.Columns.Count; j++)
{
if (dgvData.Columns[j].HeaderText == "dates")
{
DateTime dt = Convert.ToDateTime(dgvData.Rows[i].Cells[j].Value.ToString());
xCellApp.Cells[i + 2, j + 1] = dt.ToString("MM/dd/yyyy");
}
else
{
xCellApp.Cells[i + 2, j + 1] = dgvData.Rows[i].Cells[j].Value.ToString();
}
}
}
xCellApp.Columns.AutoFit();
xCellApp.Visible = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
xCellApp.Quit();
}
}
您将获得:
[{id:1, cat:'sedan'},{id:2, cat:'sport'},{id:3, cat:'sport'},{id:4, cat:'sedan'}]
答案 18 :(得分:2)
groupByArray(xs, key) {
return xs.reduce(function (rv, x) {
let v = key instanceof Function ? key(x) : x[key];
let el = rv.find((r) => r && r.key === v);
if (el) {
el.values.push(x);
}
else {
rv.push({
key: v,
values: [x]
});
}
return rv;
}, []);
}
这个输出数组。
答案 19 :(得分:2)
让我们生成一个通用的Array.prototype.groupBy()
工具。只是为了变化,让我们使用ES6 fanciness扩展运算符来进行递归方法的一些Haskellesque模式匹配。另外,让我们的Array.prototype.groupBy()
接受一个回调,它将项目(e
)索引(i
)和应用的数组(a
)作为参数。
Array.prototype.groupBy = function(cb){
return function iterate([x,...xs], i = 0, r = [[],[]]){
cb(x,i,[x,...xs]) ? (r[0].push(x), r)
: (r[1].push(x), r);
return xs.length ? iterate(xs, ++i, r) : r;
}(this);
};
var arr = [0,1,2,3,4,5,6,7,8,9],
res = arr.groupBy(e => e < 5);
console.log(res);
答案 20 :(得分:2)
Ceasar的答案很好,但仅适用于数组内部元素的内部属性(如果是字符串则为长度)。
此实现更像是:this link
const groupBy = function (arr, f) {
return arr.reduce((out, val) => {
let by = typeof f === 'function' ? '' + f(val) : val[f];
(out[by] = out[by] || []).push(val);
return out;
}, {});
};
希望这会有所帮助...
答案 21 :(得分:2)
ES6 基于reduce
的版本,支持功能iteratee
。
如果未提供iteratee
函数,则按预期方式工作:
const data = [{id: 1, score: 2},{id: 1, score: 3},{id: 2, score: 2},{id: 2, score: 4}]
const group = (arr, k) => arr.reduce((r, c) => (r[c[k]] = [...r[c[k]] || [], c], r), {});
const groupBy = (arr, k, fn = () => true) =>
arr.reduce((r, c) => (fn(c[k]) ? r[c[k]] = [...r[c[k]] || [], c] : null, r), {});
console.log(group(data, 'id')) // grouping via `reduce`
console.log(groupBy(data, 'id')) // same result if `fn` is omitted
console.log(groupBy(data, 'score', x => x > 2 )) // group with the iteratee
关于OP问题:
const data = [ { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" }, { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" }, { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" }, { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" }, { Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" }, { Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" } ]
const groupBy = (arr, k) => arr.reduce((r, c) => (r[c[k]] = [...r[c[k]] || [], c], r), {});
const groupWith = (arr, k, fn = () => true) =>
arr.reduce((r, c) => (fn(c[k]) ? r[c[k]] = [...r[c[k]] || [], c] : null, r), {});
console.log(groupBy(data, 'Phase'))
console.log(groupWith(data, 'Value', x => x > 30 )) // group by `Value` > 30
另一个 ES6 版本,该版本反转了分组并且将values
用作keys
,而将keys
用作grouped values
:
const data = [{A: "1"}, {B: "10"}, {C: "10"}]
const groupKeys = arr =>
arr.reduce((r,c) => (Object.keys(c).map(x => r[c[x]] = [...r[c[x]] || [], x]),r),{});
console.log(groupKeys(data))
注意:为了简明起见,并仅将其构想与功能以简短的形式(一行)发布。您可以展开它们并添加其他错误检查等。
答案 22 :(得分:2)
Array.prototype.groupBy = function (groupingKeyFn) {
if (typeof groupingKeyFn !== 'function') {
throw new Error("groupBy take a function as only parameter");
}
return this.reduce((result, item) => {
let key = groupingKeyFn(item);
if (!result[key])
result[key] = [];
result[key].push(item);
return result;
}, {});
}
var a = [
{type: "video", name: "a"},
{type: "image", name: "b"},
{type: "video", name: "c"},
{type: "blog", name: "d"},
{type: "video", name: "e"},
]
console.log(a.groupBy((item) => item.type));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
答案 23 :(得分:1)
让我们在重用已经编写的代码(即下划线)的同时完整回答原始问题。如果您组合它的 >100 个函数,您可以使用 Underscore 做更多的事情。以下解决方案演示了这一点。
步骤 1:通过属性的任意组合对数组中的对象进行分组。这使用了 _.groupBy
接受返回对象组的函数的事实。它还使用 _.chain
、_.pick
、_.values
、_.join
和 _.value
。请注意,此处并非严格需要 _.value
,因为链接的值在用作属性名称时会自动展开。我将它包含在内是为了防止有人试图在不发生自动解包的上下文中编写类似的代码时造成混淆。
// Given an object, return a string naming the group it belongs to.
function category(obj) {
return _.chain(obj).pick(propertyNames).values().join(' ').value();
}
// Perform the grouping.
const intermediate = _.groupBy(arrayOfObjects, category);
给定原始问题中的 arrayOfObjects
并将 propertyNames
设置为 ['Phase', 'Step']
,intermediate
将获得以下值:
{
"Phase 1 Step 1": [
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" }
],
"Phase 1 Step 2": [
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" }
],
"Phase 2 Step 1": [
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" }
],
"Phase 2 Step 2": [
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
]
}
第 2 步:将每个组缩减为单个平面对象,并将结果以数组形式返回。除了我们之前见过的函数之外,以下代码还使用了 _.pluck
、_.first
、_.pick
、_.extend
、_.reduce
和 _.map
。在这种情况下,_.first
保证返回一个对象,因为 _.groupBy
不会产生空组。在这种情况下,_.value
是必需的。
// Sum two numbers, even if they are contained in strings.
const addNumeric = (a, b) => +a + +b;
// Given a `group` of objects, return a flat object with their common
// properties and the sum of the property with name `aggregateProperty`.
function summarize(group) {
const valuesToSum = _.pluck(group, aggregateProperty);
return _.chain(group).first().pick(propertyNames).extend({
[aggregateProperty]: _.reduce(valuesToSum, addNumeric)
}).value();
}
// Get an array with all the computed aggregates.
const result = _.map(intermediate, summarize);
给定我们之前获得的 intermediate
并将 aggregateProperty
设置为 Value
,我们得到了提问者想要的 result
:
[
{ Phase: "Phase 1", Step: "Step 1", Value: 15 },
{ Phase: "Phase 1", Step: "Step 2", Value: 35 },
{ Phase: "Phase 2", Step: "Step 1", Value: 55 },
{ Phase: "Phase 2", Step: "Step 2", Value: 75 }
]
我们可以将所有这些放在一个以 arrayOfObjects
、propertyNames
和 aggregateProperty
作为参数的函数中。请注意,arrayOfObjects
实际上也可以是带有字符串键的普通对象,因为 _.groupBy
接受两者之一。为此,我已将 arrayOfObjects
重命名为 collection
。
function aggregate(collection, propertyNames, aggregateProperty) {
function category(obj) {
return _.chain(obj).pick(propertyNames).values().join(' ');
}
const addNumeric = (a, b) => +a + +b;
function summarize(group) {
const valuesToSum = _.pluck(group, aggregateProperty);
return _.chain(group).first().pick(propertyNames).extend({
[aggregateProperty]: _.reduce(valuesToSum, addNumeric)
}).value();
}
return _.chain(collection).groupBy(category).map(summarize).value();
}
aggregate(arrayOfObjects, ['Phase', 'Step'], 'Value')
现在将再次为我们提供相同的 result
。
我们可以更进一步,让调用者能够计算每个组中的值的任何统计信息。我们可以做到这一点,并且还允许调用者将任意属性添加到每个组的摘要中。我们可以做到这一切,同时让我们的代码更短。我们将 aggregateProperty
参数替换为 iteratee
参数并将其直接传递给 _.reduce
:
function aggregate(collection, propertyNames, iteratee) {
function category(obj) {
return _.chain(obj).pick(propertyNames).values().join(' ');
}
function summarize(group) {
return _.chain(group).first().pick(propertyNames)
.extend(_.reduce(group, iteratee)).value();
}
return _.chain(collection).groupBy(category).map(summarize).value();
}
实际上,我们将部分责任转移给了调用者;她必须提供一个可以传递给 iteratee
的 _.reduce
,以便对 _.reduce
的调用将生成一个具有她想要添加的聚合属性的对象。例如,我们使用以下表达式获得与之前相同的 result
:
aggregate(arrayOfObjects, ['Phase', 'Step'], (memo, value) => ({
Value: +memo.Value + +value.Value
}));
对于稍微复杂的 iteratee
的示例,假设我们要计算每个组的 最大值 Value
而不是总和,并且我们想要添加一个 Tasks
属性,用于列出组中出现的所有 Task
值。这是我们可以做到这一点的一种方法,使用上面 aggregate
的最新版本(和 _.union
):
aggregate(arrayOfObjects, ['Phase', 'Step'], (memo, value) => ({
Value: Math.max(memo.Value, value.Value),
Tasks: _.union(memo.Tasks || [memo.Task], [value.Task])
}));
我们得到以下结果:
[
{ Phase: "Phase 1", Step: "Step 1", Value: 10, Tasks: [ "Task 1", "Task 2" ] },
{ Phase: "Phase 1", Step: "Step 2", Value: 20, Tasks: [ "Task 1", "Task 2" ] },
{ Phase: "Phase 2", Step: "Step 1", Value: 30, Tasks: [ "Task 1", "Task 2" ] },
{ Phase: "Phase 2", Step: "Step 2", Value: 40, Tasks: [ "Task 1", "Task 2" ] }
]
感谢@much2learn,他还发布了一个可以处理任意归约函数的 answer。我写了几个 SO 答案,展示了如何通过组合多个 Underscore 函数来实现复杂的事情:
答案 24 :(得分:1)
在 Joseph Nields 的回答之后,有一个用于在 https://github.com/padcom/array-prototype-functions#arrayprototypegroupbyfieldormapper 中对对象进行分组的 polyfill。因此,与其一遍又一遍地编写,您可能想使用已有的内容。
答案 25 :(得分:1)
已检查答案-不是分组求解,而是直接答案。
“ REAL GROUP BY”用于按某些字段计算键名的对象数组。
const inputArray = [
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
];
var outObject = inputArray.reduce(function(a, e) {
// GROUP BY estimated key (estKey), well, may be a just plain key
// a -- Accumulator result object
// e -- sequentally checked Element, the Element that is tested just at this itaration
// new grouping name may be calculated, but must be based on real value of real field
let estKey = (e['Phase']);
(a[estKey] ? a[estKey] : (a[estKey] = null || [])).push(e);
return a;
}, {});
console.log(outObject);
玩estKey
-您可以按一个字段分组多个字段
还可以递归将数据分组。例如,最初按Phase
分组,然后按Step
字段分组。
自行验证,只需运行即可。 Этотосамоеоно,чтолюдиназываютгруппировкой?
祝你成功。
Даздравствуютвысокиепоказателимастерствапрограммистоввоимяпроцветаниявсегочеловечества! Ура,товарищи!
答案 26 :(得分:1)
groupBy
函数可以按特定键或给定分组函数对数组进行分组。打字。
groupBy = <T, K extends keyof T>(array: T[], groupOn: K | ((i: T) => string)): Record<string, T[]> => {
const groupFn = typeof groupOn === 'function' ? groupOn : (o: T) => o[groupOn];
return Object.fromEntries(
array.reduce((acc, obj) => {
const groupKey = groupFn(obj);
return acc.set(groupKey, [...(acc.get(groupKey) || []), obj]);
}, new Map())
) as Record<string, T[]>;
};
答案 27 :(得分:1)
let groupbyKeys = function(arr, ...keys) {
let keysFieldName = keys.join();
return arr.map(ele => {
let keysField = {};
keysField[keysFieldName] = keys.reduce((keyValue, key) => {
return keyValue + ele[key]
}, "");
return Object.assign({}, ele, keysField);
}).reduce((groups, ele) => {
(groups[ele[keysFieldName]] = groups[ele[keysFieldName]] || [])
.push([ele].map(e => {
if (keys.length > 1) {
delete e[keysFieldName];
}
return e;
})[0]);
return groups;
}, {});
};
console.log(groupbyKeys(array, 'Phase'));
console.log(groupbyKeys(array, 'Phase', 'Step'));
console.log(groupbyKeys(array, 'Phase', 'Step', 'Task'));
答案 28 :(得分:1)
来自@mortb,@ jmarceli回答和来自this post,
我利用JSON.stringify()
作为 PRIMITIVE VALUE 分组的多个列的标识。
function groupBy(list, keyGetter) {
const map = new Map();
list.forEach((item) => {
const key = keyGetter(item);
if (!map.has(key)) {
map.set(key, [item]);
} else {
map.get(key).push(item);
}
});
return map;
}
const pets = [
{type:"Dog", age: 3, name:"Spot"},
{type:"Cat", age: 3, name:"Tiger"},
{type:"Dog", age: 4, name:"Rover"},
{type:"Cat", age: 3, name:"Leo"}
];
const grouped = groupBy(pets,
pet => JSON.stringify({ type: pet.type, age: pet.age }));
console.log(grouped);
const pets = [
{type:"Dog", age: 3, name:"Spot"},
{type:"Cat", age: 3, name:"Tiger"},
{type:"Dog", age: 4, name:"Rover"},
{type:"Cat", age: 3, name:"Leo"}
];
let rslt = _.groupBy(pets, pet => JSON.stringify(
{ type: pet.type, age: pet.age }));
console.log(rslt);
答案 29 :(得分:1)
使用排序功能
export const groupBy = function groupByArray(xs, key, sortKey) {
return xs.reduce(function(rv, x) {
let v = key instanceof Function ? key(x) : x[key];
let el = rv.find(r => r && r.key === v);
if (el) {
el.values.push(x);
el.values.sort(function(a, b) {
return a[sortKey].toLowerCase().localeCompare(b[sortKey].toLowerCase());
});
} else {
rv.push({ key: v, values: [x] });
}
return rv;
}, []);
};
样品:
var state = [
{
name: "Arkansas",
population: "2.978M",
flag:
"https://upload.wikimedia.org/wikipedia/commons/9/9d/Flag_of_Arkansas.svg",
category: "city"
},{
name: "Crkansas",
population: "2.978M",
flag:
"https://upload.wikimedia.org/wikipedia/commons/9/9d/Flag_of_Arkansas.svg",
category: "city"
},
{
name: "Balifornia",
population: "39.14M",
flag:
"https://upload.wikimedia.org/wikipedia/commons/0/01/Flag_of_California.svg",
category: "city"
},
{
name: "Florida",
population: "20.27M",
flag:
"https://upload.wikimedia.org/wikipedia/commons/f/f7/Flag_of_Florida.svg",
category: "airport"
},
{
name: "Texas",
population: "27.47M",
flag:
"https://upload.wikimedia.org/wikipedia/commons/f/f7/Flag_of_Texas.svg",
category: "landmark"
}
];
console.log(JSON.stringify(groupBy(state,'category','name')));
答案 30 :(得分:1)
这是一个不会在null成员上破坏的ES6版本
function groupBy (arr, key) {
return (arr || []).reduce((acc, x = {}) => ({
...acc,
[x[key]]: [...acc[x[key]] || [], x]
}), {})
}
答案 31 :(得分:0)
你可以通过以下方式完成。我刚刚形成了新的数组,并从 groupBy 函数返回。通过 .map 函数
循环计算的计数var arr = [
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
];
var groupBy = (arr, pahse, step='') => {
var pahseArr = [];
var resultArr = [];
arr.map((item)=>{
var pushed = false;
pahseArr.map((ele)=>{
if(ele===item.Phase){
pushed = true;
}
})
if(!pushed){
pahseArr.push(item.Phase);
}
})
pahseArr.map((item)=>{
var sum = 0;
arr.map((ele)=>{
if(ele.Phase===item){
sum += parseFloat(ele.Value)
}
})
resultArr.push({
Phase: item,
Value: sum
})
})
if(step!=''){
var resultArr = [];
pahseArr.map((item)=>{
var stepArr = [];
arr.map((item2)=>{
var pushed = false;
stepArr.map((ele)=>{
if(ele===item2.Step){
pushed = true;
}
})
if(!pushed){
stepArr.push(item2.Step);
}
})
stepArr.map((item1)=>{
var sum = 0;
arr.map((ele)=>{
if(ele.Step===item1 && ele.Phase===item){
sum += parseFloat(ele.Value)
}
})
resultArr.push({
Phase: item,
Step: item1,
Value: sum
})
})
})
return resultArr;
}
return resultArr;
}
console.log(groupBy(arr, 'Phase'));
console.log(groupBy(arr, 'Phase', 'Step'));
答案 32 :(得分:0)
我从underscore.js fiddler
借用了这个方法window.helpers=(function (){
var lookupIterator = function(value) {
if (value == null){
return function(value) {
return value;
};
}
if (typeof value === 'function'){
return value;
}
return function(obj) {
return obj[value];
};
},
each = function(obj, iterator, context) {
var breaker = {};
if (obj == null) return obj;
if (Array.prototype.forEach && obj.forEach === Array.prototype.forEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, length = obj.length; i < length; i++) {
if (iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
var keys = []
for (var key in obj) if (Object.prototype.hasOwnProperty.call(obj, key)) keys.push(key)
for (var i = 0, length = keys.length; i < length; i++) {
if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
}
}
return obj;
},
// An internal function used for aggregate "group by" operations.
group = function(behavior) {
return function(obj, iterator, context) {
var result = {};
iterator = lookupIterator(iterator);
each(obj, function(value, index) {
var key = iterator.call(context, value, index, obj);
behavior(result, key, value);
});
return result;
};
};
return {
groupBy : group(function(result, key, value) {
Object.prototype.hasOwnProperty.call(result, key) ? result[key].push(value) : result[key] = [value];
})
};
})();
var arr=[{a:1,b:2},{a:1,b:3},{a:1,b:1},{a:1,b:2},{a:1,b:3}];
console.dir(helpers.groupBy(arr,"b"));
console.dir(helpers.groupBy(arr,function (el){
return el.b>2;
}));
答案 33 :(得分:0)
在我的特定用例中,我需要按属性分组,然后删除分组属性。
无论如何,该属性只是出于分组目的被添加到记录中,对于向用户展示没有意义。
group (arr, key) {
let prop;
return arr.reduce(function(rv, x) {
prop = x[key];
delete x[key];
(rv[prop] = (rv[prop] || [])).push(x);
return rv;
}, {});
},
感谢@caesar-bautista 提供了最佳答案中的启动功能。
答案 34 :(得分:0)
使用ES6的简单解决方案:
该方法具有返回模型,可以比较n个属性。
const compareKey = (item, key, compareItem) => {
return item[key] === compareItem[key]
}
const handleCountingRelatedItems = (listItems, modelCallback, compareKeyCallback) => {
return listItems.reduce((previousValue, currentValue) => {
if (Array.isArray(previousValue)) {
const foundIndex = previousValue.findIndex(item => compareKeyCallback(item, currentValue))
if (foundIndex > -1) {
const count = previousValue[foundIndex].count + 1
previousValue[foundIndex] = modelCallback(currentValue, count)
return previousValue
}
return [...previousValue, modelCallback(currentValue, 1)]
}
if (compareKeyCallback(previousValue, currentValue)) {
return [modelCallback(currentValue, 2)]
}
return [modelCallback(previousValue, 1), modelCallback(currentValue, 1)]
})
}
const itemList = [
{ type: 'production', human_readable: 'Production' },
{ type: 'test', human_readable: 'Testing' },
{ type: 'production', human_readable: 'Production' }
]
const model = (currentParam, count) => ({
label: currentParam.human_readable,
type: currentParam.type,
count
})
const compareParameter = (item, compareValue) => {
const isTypeEqual = compareKey(item, 'type', compareValue)
return isTypeEqual
}
const result = handleCountingRelatedItems(itemList, model, compareParameter)
console.log('Result: \n', result)
/** Result:
[
{ label: 'Production', type: 'production', count: 2 },
{ label: 'Testing', type: 'testing', count: 1 }
]
*/
答案 35 :(得分:0)
只是要添加到Scott Sauyet的answer,有些人在评论中询问如何使用他的函数来组合value1,value2等,而不是只分组一个值。
只需编辑他的和函数:
DataGrouper.register("sum", function(item) {
return _.extend({}, item.key,
{VALUE1: _.reduce(item.vals, function(memo, node) {
return memo + Number(node.VALUE1);}, 0)},
{VALUE2: _.reduce(item.vals, function(memo, node) {
return memo + Number(node.VALUE2);}, 0)}
);
});
保持主要一个(DataGrouper)不变:
var DataGrouper = (function() {
var has = function(obj, target) {
return _.any(obj, function(value) {
return _.isEqual(value, target);
});
};
var keys = function(data, names) {
return _.reduce(data, function(memo, item) {
var key = _.pick(item, names);
if (!has(memo, key)) {
memo.push(key);
}
return memo;
}, []);
};
var group = function(data, names) {
var stems = keys(data, names);
return _.map(stems, function(stem) {
return {
key: stem,
vals:_.map(_.where(data, stem), function(item) {
return _.omit(item, names);
})
};
});
};
group.register = function(name, converter) {
return group[name] = function(data, names) {
return _.map(group(data, names), converter);
};
};
return group;
}());
答案 36 :(得分:0)
var newArr = data.reduce((acc, cur) => {
const existType = acc.find(a => a.Phase === cur.Phase);
if (existType) {
existType.Value += +cur.Value;
return acc;
}
acc.push({
Phase: cur.Phase,
Value: +cur.Value
});
return acc;
}, []);
答案 37 :(得分:0)
我不认为给出的答案可以回答问题,我认为以下内容应该回答第一部分:
const arr = [
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
]
const groupBy = (key) => arr.reduce((total, currentValue) => {
const newTotal = total;
if (
total.length &&
total[total.length - 1][key] === currentValue[key]
)
newTotal[total.length - 1] = {
...total[total.length - 1],
...currentValue,
Value: parseInt(total[total.length - 1].Value) + parseInt(currentValue.Value),
};
else newTotal[total.length] = currentValue;
return newTotal;
}, []);
console.log(groupBy('Phase'));
// => [{ Phase: "Phase 1", Value: 50 },{ Phase: "Phase 2", Value: 130 }]
答案 38 :(得分:0)
发布,因为即使这个问题已有7年历史了,我仍未看到满足原始条件的答案:
我不希望它们“分裂”而是“合并”,更像是SQL group by 方法。
我之所以来到此职位是因为我想找到一种减少对象数组(例如,当您从csv读取时创建的数据结构)并通过给定索引进行聚合以生成 the相同的数据结构。我正在寻找的返回值是另一个对象数组,而不是像我在这里建议的那样嵌套对象或映射。
以下函数获取一个数据集(对象数组),一个索引列表(数组)和一个reducer函数,并返回将reducer函数作为对象数组应用于索引的结果。
function agg(data, indices, reducer) {
// helper to create unique index as an array
function getUniqueIndexHash(row, indices) {
return indices.reduce((acc, curr) => acc + row[curr], "");
}
// reduce data to single object, whose values will be each of the new rows
// structure is an object whose values are arrays
// [{}] -> {{}}
// no operation performed, simply grouping
let groupedObj = data.reduce((acc, curr) => {
let currIndex = getUniqueIndexHash(curr, indices);
// if key does not exist, create array with current row
if (!Object.keys(acc).includes(currIndex)) {
acc = {...acc, [currIndex]: [curr]}
// otherwise, extend the array at currIndex
} else {
acc = {...acc, [currIndex]: acc[currIndex].concat(curr)};
}
return acc;
}, {})
// reduce the array into a single object by applying the reducer
let reduced = Object.values(groupedObj).map(arr => {
// for each sub-array, reduce into single object using the reducer function
let reduceValues = arr.reduce(reducer, {});
// reducer returns simply the aggregates - add in the indices here
// each of the objects in "arr" has the same indices, so we take the first
let indexObj = indices.reduce((acc, curr) => {
acc = {...acc, [curr]: arr[0][curr]};
return acc;
}, {});
reduceValues = {...indexObj, ...reduceValues};
return reduceValues;
});
return reduced;
}
我将创建一个归约器,它返回count(*)和sum(Value):
reducer = (acc, curr) => {
acc.count = 1 + (acc.count || 0);
acc.value = +curr.Value + (acc.value|| 0);
return acc;
}
最后,将带有我们的reducer的agg函数应用于原始数据集会产生一个对象数组,并应用了适当的聚合:
agg(tasks, ["Phase"], reducer);
// yields:
Array(2) [
0: Object {Phase: "Phase 1", count: 4, value: 50}
1: Object {Phase: "Phase 2", count: 4, value: 130}
]
agg(tasks, ["Phase", "Step"], reducer);
// yields:
Array(4) [
0: Object {Phase: "Phase 1", Step: "Step 1", count: 2, value: 15}
1: Object {Phase: "Phase 1", Step: "Step 2", count: 2, value: 35}
2: Object {Phase: "Phase 2", Step: "Step 1", count: 2, value: 55}
3: Object {Phase: "Phase 2", Step: "Step 2", count: 2, value: 75}
]
答案 39 :(得分:0)
我的答案有所改善。此函数获取一组组字段并返回分组对象,而键也是组字段的对象。
function(xs, groupFields) {
groupFields = [].concat(groupFields);
return xs.reduce(function(rv, x) {
let groupKey = groupFields.reduce((keyObject, field) => {
keyObject[field] = x[field];
return keyObject;
}, {});
(rv[JSON.stringify(groupKey)] = rv[JSON.stringify(groupKey)] || []).push(x);
return rv;
}, {});
}
let x = [
{
"id":1,
"multimedia":false,
"language":["tr"]
},
{
"id":2,
"multimedia":false,
"language":["fr"]
},
{
"id":3,
"multimedia":true,
"language":["tr"]
},
{
"id":4,
"multimedia":false,
"language":[]
},
{
"id":5,
"multimedia":false,
"language":["tr"]
},
{
"id":6,
"multimedia":false,
"language":["tr"]
},
{
"id":7,
"multimedia":false,
"language":["tr","fr"]
}
]
groupBy(x, ['multimedia','language'])
//{
//{"multimedia":false,"language":["tr"]}: Array(3),
//{"multimedia":false,"language":["fr"]}: Array(1),
//{"multimedia":true,"language":["tr"]}: Array(1),
//{"multimedia":false,"language":[]}: Array(1),
//{"multimedia":false,"language":["tr","fr"]}: Array(1)
//}
答案 40 :(得分:0)
function groupBy(array, groupBy){
return array.reduce((acc,curr,index,array) => {
var idx = curr[groupBy];
if(!acc[idx]){
acc[idx] = array.filter(item => item[groupBy] === idx)
}
return acc;
},{})
}
// call
groupBy(items,'Step')
答案 41 :(得分:0)
data = [{id:1, name:'BMW'}, {id:2, name:'AN'}, {id:3, name:'BMW'}, {id:1, name:'NNN'}]
key = 'id'//try by id or name
data.reduce((previous, current)=>{
previous[current[key]] && previous[current[key]].length != 0 ? previous[current[key]].push(current) : previous[current[key]] = new Array(current)
return previous;
}, {})
答案 42 :(得分:0)
使用lodash库很简单
let temp = []
_.map(yourCollectionData, (row) => {
let index = _.findIndex(temp, { 'Phase': row.Phase })
if (index > -1) {
temp[index].Value += row.Value
} else {
temp.push(row)
}
})
答案 43 :(得分:0)
我会检查declarative-js groupBy
似乎完全符合您的期望。它也是:
import { Reducers } from 'declarative-js';
import groupBy = Reducers.groupBy;
import Map = Reducers.Map;
const data = [
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
];
data.reduce(groupBy(element=> element.Step), Map());
data.reduce(groupBy('Step'), Map());
答案 44 :(得分:0)
let x = [
{
"id": "6",
"name": "SMD L13",
"equipmentType": {
"id": "1",
"name": "SMD"
}
},
{
"id": "7",
"name": "SMD L15",
"equipmentType": {
"id": "1",
"name": "SMD"
}
},
{
"id": "2",
"name": "SMD L1",
"equipmentType": {
"id": "1",
"name": "SMD"
}
}
];
function groupBy(array, property) {
return array.reduce((accumulator, current) => {
const object_property = current[property];
delete current[property]
let classified_element = accumulator.find(x => x.id === object_property.id);
let other_elements = accumulator.filter(x => x.id !== object_property.id);
if (classified_element) {
classified_element.children.push(current)
} else {
classified_element = {
...object_property,
'children': [current]
}
}
return [classified_element, ...other_elements];
}, [])
}
console.log( groupBy(x, 'equipmentType') )
/* output
[
{
"id": "1",
"name": "SMD",
"children": [
{
"id": "6",
"name": "SMD L13"
},
{
"id": "7",
"name": "SMD L15"
},
{
"id": "2",
"name": "SMD L1"
}
]
}
]
*/
答案 45 :(得分:0)
这是使用ES6的令人讨厌且难以阅读的解决方案:
export default (array, key) => {
return array.reduce(
(r, v, _, __, k = v[key]) => ((r[k] || (r[k] = [])).push(v), r),
{}
);
};
答案 46 :(得分:0)
答案 47 :(得分:0)
以下功能允许对任意字段进行分组(以及求和-OP所需的值)。在解决方案中,我们定义了cmp
函数以根据分组的fields
比较两个对象。在let w=...
中,我们创建子对象x字段的副本。在y[sumBy]=+y[sumBy]+(+x[sumBy])
中,我们使用'+'将字符串转换为数字。
function groupBy(data, fields, sumBy='Value') {
let r=[], cmp= (x,y) => fields.reduce((a,b)=> a && x[b]==y[b], true);
data.forEach(x=> {
let y=r.find(z=>cmp(x,z));
let w= [...fields,sumBy].reduce((a,b) => (a[b]=x[b],a), {})
y ? y[sumBy]=+y[sumBy]+(+x[sumBy]) : r.push(w);
});
return r;
}
const d = [
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
{ Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
{ Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
{ Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
{ Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
];
function groupBy(data, fields, sumBy='Value') {
let r=[], cmp= (x,y) => fields.reduce((a,b)=> a && x[b]==y[b], true);
data.forEach(x=> {
let y=r.find(z=>cmp(x,z));
let w= [...fields,sumBy].reduce((a,b) => (a[b]=x[b],a), {})
y ? y[sumBy]=+y[sumBy]+(+x[sumBy]) : r.push(w);
});
return r;
}
// TEST
let p=(t,o) => console.log(t, JSON.stringify(o));
console.log('GROUP BY:');
p('Phase', groupBy(d,['Phase']) );
p('Step', groupBy(d,['Step']) );
p('Phase-Step', groupBy(d,['Phase', 'Step']) );
p('Phase-Task', groupBy(d,['Phase', 'Task']) );
p('Step-Task', groupBy(d,['Step', 'Task']) );
p('Phase-Step-Task', groupBy(d,['Phase','Step', 'Task']) );
答案 48 :(得分:0)
您可以在数组上使用forEach
并构造一组新的项目。这是使用FlowType
批注
// @flow
export class Group<T> {
tag: number
items: Array<T>
constructor() {
this.items = []
}
}
const groupBy = (items: Array<T>, map: (T) => number) => {
const groups = []
let currentGroup = null
items.forEach((item) => {
const tag = map(item)
if (currentGroup && currentGroup.tag === tag) {
currentGroup.items.push(item)
} else {
const group = new Group<T>()
group.tag = tag
group.items.push(item)
groups.push(group)
currentGroup = group
}
})
return groups
}
export default groupBy
开玩笑的测试可以像
// @flow
import groupBy from './groupBy'
test('groupBy', () => {
const items = [
{ name: 'January', month: 0 },
{ name: 'February', month: 1 },
{ name: 'February 2', month: 1 }
]
const groups = groupBy(items, (item) => {
return item.month
})
expect(groups.length).toBe(2)
expect(groups[1].items[1].name).toBe('February 2')
})
答案 49 :(得分:0)
我已经扩展了接受的答案,包括按多个属性分组,然后添加thenby并使其纯净而无突变。在https://stackblitz.com/edit/typescript-ezydzv
上查看演示export interface Group {
key: any;
items: any[];
}
export interface GroupBy {
keys: string[];
thenby?: GroupBy;
}
export const groupBy = (array: any[], grouping: GroupBy): Group[] => {
const keys = grouping.keys;
const groups = array.reduce((groups, item) => {
const group = groups.find(g => keys.every(key => item[key] === g.key[key]));
const data = Object.getOwnPropertyNames(item)
.filter(prop => !keys.find(key => key === prop))
.reduce((o, key) => ({ ...o, [key]: item[key] }), {});
return group
? groups.map(g => (g === group ? { ...g, items: [...g.items, data] } : g))
: [
...groups,
{
key: keys.reduce((o, key) => ({ ...o, [key]: item[key] }), {}),
items: [data]
}
];
}, []);
return grouping.thenby ? groups.map(g => ({ ...g, items: groupBy(g.items, grouping.thenby) })) : groups;
};
答案 50 :(得分:0)
基于@Ceasar Bautista的原始思想,我修改了代码并使用打字稿创建了groupBy函数。
static groupBy(data: any[], comparator: (v1: any, v2: any) => boolean, onDublicate: (uniqueRow: any, dublicateRow: any) => void) {
return data.reduce(function (reducedRows, currentlyReducedRow) {
let processedRow = reducedRows.find(searchedRow => comparator(searchedRow, currentlyReducedRow));
if (processedRow) {
// currentlyReducedRow is a dublicateRow when processedRow is not null.
onDublicate(processedRow, currentlyReducedRow)
} else {
// currentlyReducedRow is unique and must be pushed in the reducedRows collection.
reducedRows.push(currentlyReducedRow);
}
return reducedRows;
}, []);
};
此函数接受一个用于比较行并查找重复项的回调(比较器),以及一个用于汇总重复项的第二个回调(onDublicate)。
用法示例:
data = [
{ name: 'a', value: 10 },
{ name: 'a', value: 11 },
{ name: 'a', value: 12 },
{ name: 'b', value: 20 },
{ name: 'b', value: 1 }
]
private static demoComparator = (v1: any, v2: any) => {
return v1['name'] === v2['name'];
}
private static demoOnDublicate = (uniqueRow, dublicateRow) => {
uniqueRow['value'] += dublicateRow['value'];
};
通话
groupBy(data, demoComparator, demoOnDublicate)
将执行分组计算值的总和。
{name: "a", value: 33}
{name: "b", value: 21}
我们可以根据项目需要创建许多回调函数,并根据需要汇总值。例如,在一种情况下,我需要合并两个数组而不是对数据求和。
答案 51 :(得分:-1)
/**
* array group by
* @category array
* @function arrayGroupBy
* @returns {object} {"fieldName":[{...}],...}
* @static
* @author hht
* @param {string}} key group key
* @param {array} data array
*
* @example example 01
* --------------------------------------------------------------------------
* import { arrayGroupBy } from "@xx/utils";
* const array = [
* {
* type: 'assets',
* name: 'zhangsan',
* age: '33',
* },
* {
* type: 'config',
* name: 'a',
* age: '13',
* },
* {
* type: 'run',
* name: 'lisi',
* age: '3',
* },
* {
* type: 'xx',
* name: 'timo',
* age: '4',
* },
*];
* arrayGroupBy(array,'type',);
*
* result:{
* assets: [{ age: '33', name: 'zhangsan', type: 'assets' }],
* config: [{ age: '13', name: 'a', type: 'config' }],
* run: [{ age: '3', name: 'lisi', type: 'run' }],
* xx: [{ age: '4', name: 'timo', type: 'xx' }],
* };
*
* @example example 02 null
* --------------------------------------------------------------------------
* const array = null;
* arrayGroupBy(array,"type");
*
* result:{}
*
* @example example 03 key undefind
* --------------------------------------------------------------------------
* const array = [
* {
* type: 'assets',
* name: 'zhangsan',
* age: '33',
* },
* {
* type: 'config',
* name: 'a',
* age: '13',
* },
* {
* type: 'run',
* name: 'lisi',
* age: '3',
* },
* {
* type: 'xx',
* name: 'timo',
* age: '4',
* },
*];
* arrayGroupBy(array,"xx");
*
* {}
*
*/
const arrayGroupBy = (data, key) => {
if (!data || !Array.isArray(data)) return {};
const groupObj = {};
data.forEach((item) => {
if (!item[key]) return;
const fieldName = item[key];
if (!groupObj[fieldName]) {
groupObj[fieldName] = [item];
return;
}
groupObj[fieldName].push(item);
});
return groupObj;
};
const array = [
{
type: 'assets',
name: 'zhangsan',
age: '33',
},
{
type: 'config',
name: 'a',
age: '13',
},
{
type: 'run',
name: 'lisi',
age: '3',
},
{
type: 'run',
name: 'wangmazi',
age: '3',
},
{
type: 'xx',
name: 'timo',
age: '4',
},
];
console.dir(arrayGroupBy(array, 'type'))
<p>
describe('arrayGroupBy match', () => {
const array = [
{
type: 'assets',
name: 'zhangsan',
age: '33',
},
{
type: 'config',
name: 'a',
age: '13',
},
{
type: 'run',
name: 'lisi',
age: '3',
},
{
type: 'xx',
name: 'timo',
age: '4',
},
];
test('arrayGroupBy ...', () => {
const result = {
assets: [{ age: '33', name: 'zhangsan', type: 'assets' }],
config: [{ age: '13', name: 'a', type: 'config' }],
run: [{ age: '3', name: 'lisi', type: 'run' }],
xx: [{ age: '4', name: 'timo', type: 'xx' }],
};
expect(arrayGroupBy(array, 'type')).toEqual(result);
});
test('arrayGroupBy not match..', () => {
// result
expect(arrayGroupBy(array, 'xx')).toEqual({});
});
test('arrayGroupBy null', () => {
let array = null;
expect(arrayGroupBy(array, 'type')).toEqual({});
});
test('arrayGroupBy undefined', () => {
let array = undefined;
expect(arrayGroupBy(array, 'type')).toEqual({});
});
test('arrayGroupBy empty', () => {
let array = [];
expect(arrayGroupBy(array, 'type')).toEqual({});
});
});
</p>