英文:
Finding min value by two keys
问题
我有一个包含对象的数组
const data = [
{ "bankNumber": 1, "calculatedFee": 0, "feeNumber": 4, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 147, "feeNumber": 6, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 200, "feeNumber": 10, "group": "2"},
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "2" },
{ "bankNumber": 2, "calculatedFee": 10, "feeNumber": 10, "group": "3" },
{ "bankNumber": 2, "calculatedFee": 100, "feeNumber": 10, "group": "3" },
{ "bankNumber": 3, "calculatedFee": 100, "feeNumber": 10, "group": "2" },
]
我想要筛选这个数据以获取每个组和银行号中的最小值 calculatedFee
。例如,对于组1和银行号1,calculatedFee
= 0 是最小值;对于组3和银行号2,calculatedFee
= 10 是最小值。然而,我不知道在JavaScript中要创建什么样的函数来得到这样的结果。
英文:
I have array with objects
const data = [
{ "bankNumber": 1, "calculatedFee": 0, "feeNumber": 4, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 147, "feeNumber": 6, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 200, "feeNumber": 10, "group": "2"},
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "2" },
{ "bankNumber": 2, "calculatedFee": 10, "feeNumber": 10, "group": "3" },
{ "bankNumber": 2, "calculatedFee": 100, "feeNumber": 10, "group": "3" },
{ "bankNumber": 3, "calculatedFee": 100, "feeNumber": 10, "group": "2" },
]
I would like to filter this data to get the smallest value calculatedFee with one group and bankNumber. For example calculatedFee = 0 is the smallest for group 1 and bankNumber 1, calculatedFee = 10 is the smallest for group 3 and bankNumber 2,. However, I have no idea what function to create in JavaScript to get such results
答案1
得分: 1
Use Math.min()
along with filter()
and map()
const data = [
{ "bankNumber": 1, "calculatedFee": 0, "feeNumber": 4, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 147, "feeNumber": 6, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 200, "feeNumber": 10, "group": "2" },
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "2" },
{ "bankNumber": 2, "calculatedFee": 10, "feeNumber": 10, "group": "3" },
{ "bankNumber": 2, "calculatedFee": 100, "feeNumber": 10, "group": "3" },
{ "bankNumber": 3, "calculatedFee": 100, "feeNumber": 10, "group": "2" },
]
const result = Math.min(...data.filter(i => i.group === '3' && i.bankNumber === 2).map(i => i.calculatedFee))
console.log(result)
英文:
Use Math.min()
along with filter()
and map()
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const data = [
{ "bankNumber": 1, "calculatedFee": 0, "feeNumber": 4, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 147, "feeNumber": 6, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 200, "feeNumber": 10, "group": "2"},
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "2" },
{ "bankNumber": 2, "calculatedFee": 10, "feeNumber": 10, "group": "3" },
{ "bankNumber": 2, "calculatedFee": 100, "feeNumber": 10, "group": "3" },
{ "bankNumber": 3, "calculatedFee": 100, "feeNumber": 10, "group": "2" },
]
const result = Math.min(...data.filter(i => i.group === '3' && i.bankNumber === 2).map(i => i.calculatedFee))
console.log(result)
<!-- end snippet -->
答案2
得分: 0
你可以使用 Array#reduce
方法,结合一个对象来存储每个 bankNumber
和 group
组合的最小费用。
const data = [
{ bankNumber: 1, calculatedFee: 0, feeNumber: 4, group: "1" },
{ bankNumber: 1, calculatedFee: 147, feeNumber: 6, group: "1" },
{ bankNumber: 1, calculatedFee: 20, feeNumber: 10, group: "1" },
{ bankNumber: 1, calculatedFee: 200, feeNumber: 10, group: "2" },
{ bankNumber: 1, calculatedFee: 20, feeNumber: 10, group: "2" },
{ bankNumber: 2, calculatedFee: 10, feeNumber: 10, group: "3" },
{ bankNumber: 2, calculatedFee: 100, feeNumber: 10, group: "3" },
{ bankNumber: 3, calculatedFee: 100, feeNumber: 10, group: "2" },
];
const res = data.reduce((acc, { bankNumber, group, calculatedFee }) => {
(acc[bankNumber] ??= {})[group] = Math.min(acc[bankNumber][group] ?? Infinity, calculatedFee);
return acc;
}, {});
for (const [bank, vals] of Object.entries(res))
for (const [group, fee] of Object.entries(vals))
console.log('银行', bank, '分组', group, '最低费用', fee);
这段代码使用 Array#reduce
方法来计算每个银行和分组组合的最小费用,并将结果存储在对象 res
中。然后,它遍历 res
对象,输出每个银行和分组的最低费用。
英文:
You can use Array#reduce
with an object to store the minimum fee for each combination of bankNumber
and group
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const data=[{bankNumber:1,calculatedFee:0,feeNumber:4,group:"1"},{bankNumber:1,calculatedFee:147,feeNumber:6,group:"1"},{bankNumber:1,calculatedFee:20,feeNumber:10,group:"1"},{bankNumber:1,calculatedFee:200,feeNumber:10,group:"2"},{bankNumber:1,calculatedFee:20,feeNumber:10,group:"2"},{bankNumber:2,calculatedFee:10,feeNumber:10,group:"3"},{bankNumber:2,calculatedFee:100,feeNumber:10,group:"3"},{bankNumber:3,calculatedFee:100,feeNumber:10,group:"2"},];
const res = data.reduce((acc, {bankNumber, group, calculatedFee}) => {
(acc[bankNumber] ??= {})[group] = Math.min(acc[bankNumber][group] ?? Infinity, calculatedFee);
return acc;
}, {});
for (const [bank, vals] of Object.entries(res))
for (const [group, fee] of Object.entries(vals))
console.log('Bank', bank, 'Group', group, 'Min fee', fee);
<!-- end snippet -->
答案3
得分: 0
以下是您要的代码部分的翻译:
const data = [
{ "bankNumber": 1, "calculatedFee": 0, "feeNumber": 4, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 147, "feeNumber": 6, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 200, "feeNumber": 10, "group": "2" },
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "2" },
{ "bankNumber": 2, "calculatedFee": 10, "feeNumber": 10, "group": "3" },
{ "bankNumber": 2, "calculatedFee": 100, "feeNumber": 10, "group": "3" },
{ "bankNumber": 3, "calculatedFee": 100, "feeNumber": 10, "group": "2" },
]
data.sort((a, b) => {
return a.calculatedFee - b.calculatedFee;
});
console.log(data)
请注意,这是您提供的代码的翻译。如果您需要任何其他帮助,请随时告诉我。
英文:
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-js -->
const data = [
{ "bankNumber": 1, "calculatedFee": 0, "feeNumber": 4, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 147, "feeNumber": 6, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 200, "feeNumber": 10, "group": "2"},
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "2" },
{ "bankNumber": 2, "calculatedFee": 10, "feeNumber": 10, "group": "3" },
{ "bankNumber": 2, "calculatedFee": 100, "feeNumber": 10, "group": "3" },
{ "bankNumber": 3, "calculatedFee": 100, "feeNumber": 10, "group": "2" },
]
data.sort((a, b) => {
return a.calculatedFee - b.calculatedFee;
});
console.log(data)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论