Javascript 按数组中的多个属性对对象进行分组,然后将其值相加
与我的问题最接近,因为它确实通过数组中的多个键对对象进行分组。问题是,这个解决方案不求和属性值,然后删除重复项,而是将所有重复项嵌套在二维数组中 预期行为 我有一个对象数组,必须按Javascript 按数组中的多个属性对对象进行分组,然后将其值相加,javascript,arrays,duplicates,Javascript,Arrays,Duplicates,与我的问题最接近,因为它确实通过数组中的多个键对对象进行分组。问题是,这个解决方案不求和属性值,然后删除重复项,而是将所有重复项嵌套在二维数组中 预期行为 我有一个对象数组,必须按形状和颜色进行分组 var arr = [ {shape: 'square', color: 'red', used: 1, instances: 1}, {shape: 'square', color: 'red', used: 2, instances: 1}, {shape: 'circl
形状
和颜色
进行分组
var arr = [
{shape: 'square', color: 'red', used: 1, instances: 1},
{shape: 'square', color: 'red', used: 2, instances: 1},
{shape: 'circle', color: 'blue', used: 0, instances: 0},
{shape: 'square', color: 'blue', used: 4, instances: 4},
{shape: 'circle', color: 'red', used: 1, instances: 1},
{shape: 'circle', color: 'red', used: 1, instances: 0},
{shape: 'square', color: 'blue', used: 4, instances: 5},
{shape: 'square', color: 'red', used: 2, instances: 1}
];
仅当此数组中的对象的形状
和颜色
相同时,才会将其视为重复对象。如果是,我想分别总结它们的已用
和实例
值,然后删除重复项
因此,在此示例中,结果数组可能只包含四种组合:方红色
,方蓝色
,圆红色
,圆蓝色
问题
我在这里尝试了一种更简单的方法:
var-arr=[
{shape:'square',color:'red',used:1,instances:1},
{形状:'square',颜色:'red',用法:2,实例:1},
{shape:'circle',color:'blue',used:0,instances:0},
{形状:'square',颜色:'blue',用法:4,实例:4},
{形状:“圆形”,颜色:“红色”,使用:1,实例:1},
{形状:“圆形”,颜色:“红色”,使用:1,实例:0},
{形状:'square',颜色:'red',用法:4,实例:4},
{形状:'square',颜色:'red',用法:2,实例:2}
];
结果=[];
arr.forEach(功能(a){
如果(!this[a.color]&&!this[a.shape]){
this[a.color]={color:a.color,shape:a.shape,used:0,instances:0};
结果。推送(此[a.颜色]);
}
此[a.color].used+=a.used;
此[a.color]。实例+=a.instances;
},Object.create(null));
控制台日志(结果)代码>与辅助对象一起使用可对类似对象进行分组。对于每个对象,检查辅助对象中是否存在组合的形状
和颜色
。如果没有,请使用添加到辅助对象以创建对象的副本,然后推送到数组。如果是,则将其值添加到已使用的
和实例
var arr=[{“形状”:“正方形”,“颜色”:“红色”,“使用过”:1,“实例”:1},{“形状”:“正方形”,“颜色”:“红色”,“使用过”:2,“实例”:1},{“形状”:“圆形”,“颜色”:“蓝色”,“使用过”:0,“实例”:0},{“形状”:“正方形”,“颜色”:“蓝色”,“使用过”:4,“实例”:4},{“形状”:“圆形”,“颜色”:“红色”,“使用过”:1,“实例”:1},{“形状”:“圆形”,“颜色”:“红色”,“使用过”:1,“实例”:0},{“形状”:“正方形”,“颜色”:“蓝色”,“使用过”:4,“实例”:5},{“形状”:“正方形”,“颜色”:“红色”,“使用过”:2,“实例”:1}];
var-helper={};
var结果=arr.REDUCT(函数(r,o){
var key=o.shape+'-'+o.color;
如果(!helper[key]){
helper[key]=Object.assign({},o);//创建o的副本
r、 推(助手[键]);
}否则{
helper[key].used+=o.used;
helper[key].instances+=o.instances;
}
返回r;
}, []);
console.log(result);
您可以使用reduce()
创建一个具有唯一shape | color
属性的对象,并使用object.values()
返回这些值的数组
var arr=[{“形状”:“方形”,“颜色”:“红色”,“使用过”:1,“实例”:1},{“形状”:“方形”,“颜色”:“红色”,“使用过”:1,“实例”:1},{“形状”:“圆形”,“颜色”:“蓝色”,“使用过”:0,“实例”:0},{“形状”:“方形”,“颜色”:“蓝色”,“使用过”:4,“实例”:4},{“形状”:“圆形”,“颜色”:“红色”,“使用过”:1,“实例”:1,{“形状”:“圆形”,“颜色”:“使用过”:1,“实例”“:0},{”形状“:”正方形“,”颜色“:”蓝色“,”已用“:4,“实例“:5},{”形状“:”正方形“,”颜色“:”红色“,”已用“:2,“实例“:1}]
var结果=对象值(arr.reduce)(函数(r,e){
var key=e.shape+'|'+e.color;
如果(!r[key])r[key]=e;
否则{
r[key].used+=e.used;
r[key].instances+=e.instances
}
返回r;
}, {}))
log(result)
您可以使用哈希表和键对相同的组进行分组
var数组=[{shape:'square',color:'red',used:1},{shape:'square',color:'red',used:2,instances:1},{shape:'circle',color:'blue',used:0,instances:0},{shape:'square',color:'blue',used:4,instances:4},{shape:'circle',color:'red used:1,instances:1},{shape:'circle',color:'red',used:1,实例:0},{shape:'square',color:'blue',used:4,实例:5},{shape:'square',color:'red',used:2,实例:1}],
hash=Object.create(null),
分组=[];
array.forEach(函数(o){
var key=['shape','color'].map(函数(k){returno[k];}).join('|');
如果(!哈希[键]){
hash[key]={shape:o.shape,color:o.color,used:0,instances:0};
push(hash[key]);
}
forEach(函数(k){hash[key][k]+=o[k];});
});
console.log(分组);
.as控制台包装{max height:100%!important;top:0;}
我有一个建议给你。
如果要使操作更简单,请尝试使用下划线库:
我迅速尝试使用它,并得到了正确的结果:
var arr = [
{shape: 'square', color: 'red', used: 1, instances: 1},
{shape: 'square', color: 'red', used: 2, instances: 1},
{shape: 'circle', color: 'blue', used: 0, instances: 0},
{shape: 'square', color: 'blue', used: 4, instances: 4},
{shape: 'circle', color: 'red', used: 1, instances: 1},
{shape: 'circle', color: 'red', used: 1, instances: 0},
{shape: 'square', color: 'blue', used: 4, instances: 5},
{shape: 'square', color: 'red', used: 2, instances: 1}
];
var byshape = _.groupBy(arr, 'shape');
var bycolor = _.map(byshape, function(array) {
return _.groupBy(array, 'color')
});
var output = [];
_.each(bycolor, function(arrayOfShape) {
_.each(arrayOfShape, function(arrayOfColor) {
var computedItem = {shape: "", color: "", used: 0, instances: 0};
_.each(arrayOfColor, function(item) {
computedItem.shape = item.shape;
computedItem.color = item.color;
computedItem.used += item.used;
computedItem.instances += item.instances;
});
output.push(computedItem);
});
});
console.log(output);
此解决方案首先对数据进行分组,然后您可以根据需要执行操作,例如,根据ypur的意愿计算数据
也许你可以优化它,如果你需要更多的帮助,请告诉我
使用此方法可以指定多个属性:
调用此方法,如下所示:
调用函数:
const groupResult = groupBy( {Group: data, By: groupByProperties} );
小组的结果是:
(6) [Array(2), Array(5), Array(3), Array(3), Array(1), Array(1)]
0: Array(2)
0: {Name: "George", Surname: "Best", Country: "Great Britain", Gender: "Male", Money: 8000}
1: {Name: "George", Surname: "Washington", Country: "Great Britain", Gender: "Male", Money: 4200}
length: 2
__proto__: Array(0)
1: Array(5)
0: {Name: "Orion", Surname: "Papathanasiou", Country: "Greece", Gender: "Male", Money: 2000}
1: {Name: "Thanasis", Surname: "Papathanasiou", Country: "Greece", Gender: "Male", Money: 3200}
2: {Name: "Orfeas", Surname: "Kalaitzis", Country: "Greece", Gender: "Male", Money: 7643}
3: {Name: "Kostas", Surname: "Antoniou", Country: "Greece", Gender: "Male", Money: 8712}
4: {Name: "Paulos", Surname: "Stamou", Country: "Greece", Gender: "Male", Money: 3422}
length: 5
__proto__: Array(0)
2: Array(3)
0: {Name: "Mairy", Surname: "Wellbeck", Country: "Great Britain", Gender: "Female", Money: 5000}
1: {Name: "Helen", Surname: "Smith", Country: "Great Britain", Gender: "Female", Money: 1000}
2: {Name: "Cathrine", Surname: "Bryant", Country: "Great Britain", Gender: "Female", Money: 8712}
length: 3
__proto__: Array(0)
3: Array(3)
0: {Name: "Nick", Surname: "Wellington", Country: "USA", Gender: "Male", Money: 1000}
1: {Name: "John", Surname: "Oneal", Country: "USA", Gender: "Male", Money: 98234}
2: {Name: "Paul", Surname: "Pierce", Country: "USA", Gender: "Male", Money: 13000}
length: 3
__proto__: Array(0)
4: Array(1)
0: {Name: "Soula", Surname: "Spuropoulou", Country: "Greece", Gender: "Female", Money: 400}
length: 1
__proto__: Array(0)
5: Array(1)
0: {Name: "Jenny", Surname: "Scalabrini", Country: "USA", Gender: "Female", Money: 92214}
length: 1
__proto__: Array(0)
length: 6
__proto__: Array(0)
因此,我们得到了6个数组。每个数组按国家和性别进行分组
迭代每个数组,我们就可以算出钱的总数了
constgroupby=({Group:array,By:props})=>{
getGroupedItems=(项目)=>{
returnArray=[];
让我;
对于(i=0;i/**
* Groups an array of objects with multiple properties.
*
* @param {Array} array: the array of objects to group
* @param {Array} props: the properties to groupby
* @return {Array} an array of arrays with the grouped results
*/
const groupBy = ({ Group: array, By: props }) => {
getGroupedItems = (item) => {
returnArray = [];
let i;
for (i = 0; i < props.length; i++) {
returnArray.push(item[props[i]]);
}
return returnArray;
};
let groups = {};
let i;
for (i = 0; i < array.length; i++) {
const arrayRecord = array[i];
const group = JSON.stringify(getGroupedItems(arrayRecord));
groups[group] = groups[group] || [];
groups[group].push(arrayRecord);
}
return Object.keys(groups).map((group) => {
return groups[group];
});
};
const data = [
{Name: 'George', Surname: 'Best', Country: 'Great Britain', Gender: 'Male', Money:8000},
{Name: 'Orion', Surname: 'Papathanasiou', Country: 'Greece', Gender: 'Male', Money: 2000},
{Name: 'Mairy', Surname: 'Wellbeck', Country: 'Great Britain', Gender: 'Female', Money:5000},
{Name: 'Thanasis', Surname: 'Papathanasiou', Country: 'Greece',Gender: 'Male', Money: 3200},
{Name: 'George', Surname: 'Washington', Country: 'Great Britain', Gender: 'Male',Money:4200},
{Name: 'Orfeas', Surname: 'Kalaitzis', Country: 'Greece', Gender: 'Male', Money: 7643},
{Name: 'Nick', Surname: 'Wellington', Country: 'USA', Gender: 'Male', Money:1000},
{Name: 'Kostas', Surname: 'Antoniou', Country: 'Greece', Gender: 'Male', Money: 8712},
{Name: 'John', Surname: 'Oneal', Country: 'USA', Gender: 'Male', Money:98234},
{Name: 'Paulos', Surname: 'Stamou', Country: 'Greece', Gender: 'Male', Money: 3422},
{Name: 'Soula', Surname: 'Spuropoulou', Country: 'Greece', Gender: 'Female', Money:400},
{Name: 'Paul', Surname: 'Pierce', Country: 'USA', Gender: 'Male',Money: 13000},
{Name: 'Helen', Surname: 'Smith', Country: 'Great Britain', Gender: 'Female', Money:1000},
{Name: 'Cathrine', Surname: 'Bryant', Country: 'Great Britain', Gender: 'Female', Money: 8712},
{Name: 'Jenny', Surname: 'Scalabrini', Country: 'USA', Gender: 'Female', Money:92214}];
const groupByProperties = ['Country', 'Gender'];
const groupResult = groupBy( {Group: data, By: groupByProperties} );
(6) [Array(2), Array(5), Array(3), Array(3), Array(1), Array(1)]
0: Array(2)
0: {Name: "George", Surname: "Best", Country: "Great Britain", Gender: "Male", Money: 8000}
1: {Name: "George", Surname: "Washington", Country: "Great Britain", Gender: "Male", Money: 4200}
length: 2
__proto__: Array(0)
1: Array(5)
0: {Name: "Orion", Surname: "Papathanasiou", Country: "Greece", Gender: "Male", Money: 2000}
1: {Name: "Thanasis", Surname: "Papathanasiou", Country: "Greece", Gender: "Male", Money: 3200}
2: {Name: "Orfeas", Surname: "Kalaitzis", Country: "Greece", Gender: "Male", Money: 7643}
3: {Name: "Kostas", Surname: "Antoniou", Country: "Greece", Gender: "Male", Money: 8712}
4: {Name: "Paulos", Surname: "Stamou", Country: "Greece", Gender: "Male", Money: 3422}
length: 5
__proto__: Array(0)
2: Array(3)
0: {Name: "Mairy", Surname: "Wellbeck", Country: "Great Britain", Gender: "Female", Money: 5000}
1: {Name: "Helen", Surname: "Smith", Country: "Great Britain", Gender: "Female", Money: 1000}
2: {Name: "Cathrine", Surname: "Bryant", Country: "Great Britain", Gender: "Female", Money: 8712}
length: 3
__proto__: Array(0)
3: Array(3)
0: {Name: "Nick", Surname: "Wellington", Country: "USA", Gender: "Male", Money: 1000}
1: {Name: "John", Surname: "Oneal", Country: "USA", Gender: "Male", Money: 98234}
2: {Name: "Paul", Surname: "Pierce", Country: "USA", Gender: "Male", Money: 13000}
length: 3
__proto__: Array(0)
4: Array(1)
0: {Name: "Soula", Surname: "Spuropoulou", Country: "Greece", Gender: "Female", Money: 400}
length: 1
__proto__: Array(0)
5: Array(1)
0: {Name: "Jenny", Surname: "Scalabrini", Country: "USA", Gender: "Female", Money: 92214}
length: 1
__proto__: Array(0)
length: 6
__proto__: Array(0)
var arr = [
{shape: 'square', color: 'red', used: 1, instances: 1},
{shape: 'square', color: 'red', used: 2, instances: 1},
{shape: 'circle', color: 'blue', used: 0, instances: 0},
{shape: 'square', color: 'blue', used: 4, instances: 4},
{shape: 'circle', color: 'red', used: 1, instances: 1},
{shape: 'circle', color: 'red', used: 1, instances: 0},
{shape: 'square', color: 'blue', used: 4, instances: 5},
{shape: 'square', color: 'red', used: 2, instances: 1}
];
result = [];
arr.forEach(function (a) {
if ( !this[a.color] && !this[a.shape] ) {
this[a.color] = { color: a.color, shape: a.shape, used: 0, instances: 0 };
result.push(this[a.color]);
}
this[a.color].used += a.used;
this[a.color].instances += a.instances;
}, Object.create(null));
console.log(result);
**Output:**
[
{
"color": "red",
"shape": "square",
"used": 11,
"instances": 9
},
{
"color": "blue",
"shape": "circle",
"used": 4,
"instances": 4
}
]
thats all perfetcly working.
Enjoy your coding....
// To call this function:
// const result = this.toolBox.multipleGroupByArray(
// dataArray, (property: IProperty) => [property.prop1, property.prop2, property.prop3]);
multipleGroupByArray(dataArray, groupPropertyArray) {
const groups = {};
dataArray.forEach(item => {
const group = JSON.stringify(groupPropertyArray(item));
groups[group] = groups[group] || [];
groups[group].push(item);
});
return Object.keys(groups).map(function(group) {
return groups[group];
});
}
function groupBy(array, f) {
let groups = {};
array.forEach((o) => {
var group = f(o).join('-');
groups[group] = groups[group] || [];
groups[group].push(o);
});
return groups;
}
groupBy(connectedServers, (item) => {
return [item.key1, item.key2];
});
function groupAndSum(arr, groupKeys, sumKeys){
return Object.values(
arr.reduce((acc,curr)=>{
const group = groupKeys.map(k => curr[k]).join('-');
acc[group] = acc[group] || Object.fromEntries(
groupKeys.map(k => [k, curr[k]]).concat(sumKeys.map(k => [k, 0])));
sumKeys.forEach(k => acc[group][k] += curr[k]);
return acc;
}, {})
);
}
var array = [{ shape: 'square', color: 'red', used: 1, instances: 1 }, { shape: 'square', color: 'red', used: 2, instances: 1 }, { shape: 'circle', color: 'blue', used: 0, instances: 0 }, { shape: 'square', color: 'blue', used: 4, instances: 4 }, { shape: 'circle', color: 'red', used: 1, instances: 1 }, { shape: 'circle', color: 'red', used: 1, instances: 0 }, { shape: 'square', color: 'blue', used: 4, instances: 5 }, { shape: 'square', color: 'red', used: 2, instances: 1 }],
hash = Object.create(null),
grouped = [];
array.forEach(function (o) {
var key = ['shape', 'color'].map(function (k) { return o[k]; }).join('|');
if (!hash[key]) {
hash[key] = { shape: o.shape, color: o.color, YourArrayName : [] };
grouped.push(hash[key]);
}
['used'].forEach(function (k) { hash[key]['YourArrayName'].push({ used : o['used'], instances : o['instances'] }) });
});
console.log(grouped);