如何根据属性的总和来组织对象数组?
How do I organize an Array of objects according to the sum of its attributes?
我正在为一个销售应用程序开发后端部分,其中我有 2 个对象数组,一个是卖家的名字,另一个是月销售额。
我的上级告诉我,使用包含销售额的数组中的数据,从销售额最高的名称到销售额最少的名称(从最高到最低)排序名称数组,基本上是对键=>数量。
我不知道该怎么做,我尝试对每个卖家的销售额使用 Reduce 方法,但我想不出如何比较它们来重组数组。
代码是:
const sellers= [{id:1,name: 'juan', age: 23},{id:2,name: 'adrian', age: 32},{id:3,name: 'apolo', age: 45}];
const sales= [{equipo: 'frances', sellerId: 2, quantity: 234},{equipo: 'italiano', sellerId: 3, quantity: 24},{equipo: 'polaco', sellerId: 1, quantity: 534},{equipo: 'frances', sellerId: 2, quantity: 1234},{equipo: 'frances', sellerId: 3, quantity: 2342}];
这是我已经尝试过的:
const bestSellers= () => {
sales.reduce((sum, value) => ( value.sellerId == 1 ? sum + value.area : sum), 0); }
最终结果应该是这样的:
const sellers= [{id:3,name: 'apolo', age: 45},{id:2,name: 'adrian', age: 32},{id:1,name: 'juan', age: 23}]
你想在这里做两件事。
- 找出每个卖家的总销售额。
- 对每个卖家的总销售额进行排序。
在我的排序功能中,您可以看到我正在筛选卖家的所有销售。
一旦我只有一个卖家的销售额,我就使用 reduce 方法将他们的销售额汇总为一个易于使用的数字。
然后我使用排序方法将以前的卖家数量与当前卖家数量进行比较re-order。
我鼓励您阅读所用方法的文档,以便了解每一步发生的情况。
const sellers = [{
id: 1,
name: 'juan',
age: 23
}, {
id: 2,
name: 'adrian',
age: 32
}, {
id: 3,
name: 'apolo',
age: 45
}];
const sales = [{
equipo: 'frances',
sellerId: 2,
quantity: 234
}, {
equipo: 'italiano',
sellerId: 3,
quantity: 24
}, {
equipo: 'polaco',
sellerId: 1,
quantity: 534
}, {
equipo: 'frances',
sellerId: 2,
quantity: 1234
}, {
equipo: 'frances',
sellerId: 3,
quantity: 2342
}];
const expected = [{
id: 3,
name: 'apolo',
age: 45
}, {
id: 2,
name: 'adrian',
age: 32
}, {
id: 1,
name: 'juan',
age: 23
}]
const result = sellers.sort((a, b) => {
totalA = sales.filter(sale => sale.sellerId === a.id).reduce((acc, val) => acc + val.quantity, 0)
totalB = sales.filter(sale => sale.sellerId === b.id).reduce((acc, val) => acc + val.quantity, 0)
return totalB - totalA
})
// Check we get what we expect
console.log(JSON.stringify(expected) === JSON.stringify(result))
首先,将 sales
数组缩减为每个卖家的数量总和,对该列表进行排序,然后映射到 sellers
数组中的相应卖家。
const sellers= [{id:1,name: 'juan', age: 23},{id:2,name: 'adrian', age: 32},{id:3,name: 'apolo', age: 45}];
const sales= [{equipo: 'frances', sellerId: 2, quantity: 234},{equipo: 'italiano', sellerId: 3, quantity: 24},{equipo: 'polaco', sellerId: 1, quantity: 534},{equipo: 'frances', sellerId: 2, quantity: 1234},{equipo: 'frances', sellerId: 3, quantity: 2342}];
const bestSellers = (sellers, sales) => {
let res = sales.reduce((result, sale) => {
// Check if seller is already added in result
const obj = result.find(s => s.sellerId === sale.sellerId);
if (obj) {
// If seller already added then increase it's quantity
obj.quantity += sale.quantity;
} else {
// If seller not added, add seller in result
result.push({ sellerId: sale.sellerId, quantity: sale.quantity });
}
return result;
}, []);
// Sort result by quantity in decreasing order
res.sort((a,b) => b.quantity - a.quantity);
// Map each sale to the seller
res = res.map(s => sellers.find(seller => seller.id === s.sellerId));
return res;
}
const result = bestSellers(sellers, sales);
console.log(result);
你可以做到:
const
sellers =
[ { id: 1, name: 'juan', age: 23 }
, { id: 2, name: 'adrian', age: 32 }
, { id: 3, name: 'apolo', age: 45 }
]
, sales =
[ { equipo: 'frances', sellerId: 2, quantity: 234 }
, { equipo: 'italiano', sellerId: 3, quantity: 24 }
, { equipo: 'polaco', sellerId: 1, quantity: 534 }
, { equipo: 'frances', sellerId: 2, quantity: 1234 }
, { equipo: 'frances', sellerId: 3, quantity: 2342 }
];
sellers.forEach( ({id},i,all) => // add a sum attribute
{
all[i].sum = sales
.filter(({sellerId})=>sellerId===id)
.reduce((sum,{quantity})=>sum+quantity,0)
});
sellers
.sort( (a,b) => b.sum - a.sum)
.forEach((seller,i,all) => delete seller.sum ) // remove the sum
console.log( sellers )
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}
或:
const
sellers =
[ { id: 1, name: 'juan', age: 23 }
, { id: 2, name: 'adrian', age: 32 }
, { id: 3, name: 'apolo', age: 45 }
]
, sales =
[ { equipo: 'frances', sellerId: 2, quantity: 234 }
, { equipo: 'italiano', sellerId: 3, quantity: 24 }
, { equipo: 'polaco', sellerId: 1, quantity: 534 }
, { equipo: 'frances', sellerId: 2, quantity: 1234 }
, { equipo: 'frances', sellerId: 3, quantity: 2342 }
]
// compute all the sum in a new Sums object:
const Sums = sales.reduce((sum,{sellerId : id, quantity}) =>
( sum[id] ??= 0, sum[id] += quantity , sum ), {} )
sellers .sort( (a,b) => Sums[b.id] - Sums[a.id])
console.log( sellers )
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}
你可以这样做:
const sellers = [{ id: 1, name: 'juan', age: 23 },{ id: 2, name: 'adrian', age: 32 },{ id: 3, name: 'apolo', age: 45 },]
const sales = [{ equipo: 'frances', sellerId: 2, quantity: 234 },{ equipo: 'italiano', sellerId: 3, quantity: 24 },{ equipo: 'polaco', sellerId: 1, quantity: 534 },{ equipo: 'frances', sellerId: 2, quantity: 1234 },{ equipo: 'frances', sellerId: 3, quantity: 2342 },]
const salesHash = sales.reduce((a, { sellerId, quantity }) =>
(a[sellerId] = (a[sellerId] || 0) + quantity, a), {})
const result = sellers.sort((a, b) =>
salesHash[b.id] - salesHash[a.id])
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
我正在为一个销售应用程序开发后端部分,其中我有 2 个对象数组,一个是卖家的名字,另一个是月销售额。 我的上级告诉我,使用包含销售额的数组中的数据,从销售额最高的名称到销售额最少的名称(从最高到最低)排序名称数组,基本上是对键=>数量。 我不知道该怎么做,我尝试对每个卖家的销售额使用 Reduce 方法,但我想不出如何比较它们来重组数组。 代码是:
const sellers= [{id:1,name: 'juan', age: 23},{id:2,name: 'adrian', age: 32},{id:3,name: 'apolo', age: 45}];
const sales= [{equipo: 'frances', sellerId: 2, quantity: 234},{equipo: 'italiano', sellerId: 3, quantity: 24},{equipo: 'polaco', sellerId: 1, quantity: 534},{equipo: 'frances', sellerId: 2, quantity: 1234},{equipo: 'frances', sellerId: 3, quantity: 2342}];
这是我已经尝试过的:
const bestSellers= () => {
sales.reduce((sum, value) => ( value.sellerId == 1 ? sum + value.area : sum), 0); }
最终结果应该是这样的:
const sellers= [{id:3,name: 'apolo', age: 45},{id:2,name: 'adrian', age: 32},{id:1,name: 'juan', age: 23}]
你想在这里做两件事。
- 找出每个卖家的总销售额。
- 对每个卖家的总销售额进行排序。
在我的排序功能中,您可以看到我正在筛选卖家的所有销售。
一旦我只有一个卖家的销售额,我就使用 reduce 方法将他们的销售额汇总为一个易于使用的数字。
然后我使用排序方法将以前的卖家数量与当前卖家数量进行比较re-order。
我鼓励您阅读所用方法的文档,以便了解每一步发生的情况。
const sellers = [{
id: 1,
name: 'juan',
age: 23
}, {
id: 2,
name: 'adrian',
age: 32
}, {
id: 3,
name: 'apolo',
age: 45
}];
const sales = [{
equipo: 'frances',
sellerId: 2,
quantity: 234
}, {
equipo: 'italiano',
sellerId: 3,
quantity: 24
}, {
equipo: 'polaco',
sellerId: 1,
quantity: 534
}, {
equipo: 'frances',
sellerId: 2,
quantity: 1234
}, {
equipo: 'frances',
sellerId: 3,
quantity: 2342
}];
const expected = [{
id: 3,
name: 'apolo',
age: 45
}, {
id: 2,
name: 'adrian',
age: 32
}, {
id: 1,
name: 'juan',
age: 23
}]
const result = sellers.sort((a, b) => {
totalA = sales.filter(sale => sale.sellerId === a.id).reduce((acc, val) => acc + val.quantity, 0)
totalB = sales.filter(sale => sale.sellerId === b.id).reduce((acc, val) => acc + val.quantity, 0)
return totalB - totalA
})
// Check we get what we expect
console.log(JSON.stringify(expected) === JSON.stringify(result))
首先,将 sales
数组缩减为每个卖家的数量总和,对该列表进行排序,然后映射到 sellers
数组中的相应卖家。
const sellers= [{id:1,name: 'juan', age: 23},{id:2,name: 'adrian', age: 32},{id:3,name: 'apolo', age: 45}];
const sales= [{equipo: 'frances', sellerId: 2, quantity: 234},{equipo: 'italiano', sellerId: 3, quantity: 24},{equipo: 'polaco', sellerId: 1, quantity: 534},{equipo: 'frances', sellerId: 2, quantity: 1234},{equipo: 'frances', sellerId: 3, quantity: 2342}];
const bestSellers = (sellers, sales) => {
let res = sales.reduce((result, sale) => {
// Check if seller is already added in result
const obj = result.find(s => s.sellerId === sale.sellerId);
if (obj) {
// If seller already added then increase it's quantity
obj.quantity += sale.quantity;
} else {
// If seller not added, add seller in result
result.push({ sellerId: sale.sellerId, quantity: sale.quantity });
}
return result;
}, []);
// Sort result by quantity in decreasing order
res.sort((a,b) => b.quantity - a.quantity);
// Map each sale to the seller
res = res.map(s => sellers.find(seller => seller.id === s.sellerId));
return res;
}
const result = bestSellers(sellers, sales);
console.log(result);
你可以做到:
const
sellers =
[ { id: 1, name: 'juan', age: 23 }
, { id: 2, name: 'adrian', age: 32 }
, { id: 3, name: 'apolo', age: 45 }
]
, sales =
[ { equipo: 'frances', sellerId: 2, quantity: 234 }
, { equipo: 'italiano', sellerId: 3, quantity: 24 }
, { equipo: 'polaco', sellerId: 1, quantity: 534 }
, { equipo: 'frances', sellerId: 2, quantity: 1234 }
, { equipo: 'frances', sellerId: 3, quantity: 2342 }
];
sellers.forEach( ({id},i,all) => // add a sum attribute
{
all[i].sum = sales
.filter(({sellerId})=>sellerId===id)
.reduce((sum,{quantity})=>sum+quantity,0)
});
sellers
.sort( (a,b) => b.sum - a.sum)
.forEach((seller,i,all) => delete seller.sum ) // remove the sum
console.log( sellers )
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}
或:
const
sellers =
[ { id: 1, name: 'juan', age: 23 }
, { id: 2, name: 'adrian', age: 32 }
, { id: 3, name: 'apolo', age: 45 }
]
, sales =
[ { equipo: 'frances', sellerId: 2, quantity: 234 }
, { equipo: 'italiano', sellerId: 3, quantity: 24 }
, { equipo: 'polaco', sellerId: 1, quantity: 534 }
, { equipo: 'frances', sellerId: 2, quantity: 1234 }
, { equipo: 'frances', sellerId: 3, quantity: 2342 }
]
// compute all the sum in a new Sums object:
const Sums = sales.reduce((sum,{sellerId : id, quantity}) =>
( sum[id] ??= 0, sum[id] += quantity , sum ), {} )
sellers .sort( (a,b) => Sums[b.id] - Sums[a.id])
console.log( sellers )
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}
你可以这样做:
const sellers = [{ id: 1, name: 'juan', age: 23 },{ id: 2, name: 'adrian', age: 32 },{ id: 3, name: 'apolo', age: 45 },]
const sales = [{ equipo: 'frances', sellerId: 2, quantity: 234 },{ equipo: 'italiano', sellerId: 3, quantity: 24 },{ equipo: 'polaco', sellerId: 1, quantity: 534 },{ equipo: 'frances', sellerId: 2, quantity: 1234 },{ equipo: 'frances', sellerId: 3, quantity: 2342 },]
const salesHash = sales.reduce((a, { sellerId, quantity }) =>
(a[sellerId] = (a[sellerId] || 0) + quantity, a), {})
const result = sellers.sort((a, b) =>
salesHash[b.id] - salesHash[a.id])
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }