找到两个键的最小值

huangapple go评论71阅读模式
英文:

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 = [
  { &quot;bankNumber&quot;: 1, &quot;calculatedFee&quot;:   0, &quot;feeNumber&quot;:  4, &quot;group&quot;: &quot;1&quot; },
  { &quot;bankNumber&quot;: 1, &quot;calculatedFee&quot;: 147, &quot;feeNumber&quot;:  6, &quot;group&quot;: &quot;1&quot; },
  { &quot;bankNumber&quot;: 1, &quot;calculatedFee&quot;:  20, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;1&quot; },
  { &quot;bankNumber&quot;: 1, &quot;calculatedFee&quot;:  200, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;2&quot;},
  { &quot;bankNumber&quot;: 1, &quot;calculatedFee&quot;:  20, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;2&quot; },
  { &quot;bankNumber&quot;: 2, &quot;calculatedFee&quot;:  10, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;3&quot; },
  { &quot;bankNumber&quot;: 2, &quot;calculatedFee&quot;: 100, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;3&quot; },
  { &quot;bankNumber&quot;: 3, &quot;calculatedFee&quot;: 100, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;2&quot; },
]

const result = Math.min(...data.filter(i =&gt; i.group === &#39;3&#39; &amp;&amp; i.bankNumber === 2).map(i =&gt; i.calculatedFee))

console.log(result)

<!-- end snippet -->

答案2

得分: 0

你可以使用 Array#reduce 方法,结合一个对象来存储每个 bankNumbergroup 组合的最小费用。

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:&quot;1&quot;},{bankNumber:1,calculatedFee:147,feeNumber:6,group:&quot;1&quot;},{bankNumber:1,calculatedFee:20,feeNumber:10,group:&quot;1&quot;},{bankNumber:1,calculatedFee:200,feeNumber:10,group:&quot;2&quot;},{bankNumber:1,calculatedFee:20,feeNumber:10,group:&quot;2&quot;},{bankNumber:2,calculatedFee:10,feeNumber:10,group:&quot;3&quot;},{bankNumber:2,calculatedFee:100,feeNumber:10,group:&quot;3&quot;},{bankNumber:3,calculatedFee:100,feeNumber:10,group:&quot;2&quot;},];
const res = data.reduce((acc, {bankNumber, group, calculatedFee}) =&gt; {
  (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(&#39;Bank&#39;, bank, &#39;Group&#39;, group, &#39;Min fee&#39;, 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 = [
    { &quot;bankNumber&quot;: 1, &quot;calculatedFee&quot;:   0, &quot;feeNumber&quot;:  4, &quot;group&quot;: &quot;1&quot; },
    { &quot;bankNumber&quot;: 1, &quot;calculatedFee&quot;: 147, &quot;feeNumber&quot;:  6, &quot;group&quot;: &quot;1&quot; },
    { &quot;bankNumber&quot;: 1, &quot;calculatedFee&quot;:  20, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;1&quot; },
    { &quot;bankNumber&quot;: 1, &quot;calculatedFee&quot;:  200, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;2&quot;},
    { &quot;bankNumber&quot;: 1, &quot;calculatedFee&quot;:  20, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;2&quot; },
    { &quot;bankNumber&quot;: 2, &quot;calculatedFee&quot;:  10, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;3&quot; },
    { &quot;bankNumber&quot;: 2, &quot;calculatedFee&quot;: 100, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;3&quot; },
    { &quot;bankNumber&quot;: 3, &quot;calculatedFee&quot;: 100, &quot;feeNumber&quot;: 10, &quot;group&quot;: &quot;2&quot; },
  ]
  data.sort((a, b) =&gt; {
    return a.calculatedFee - b.calculatedFee;
    
  });
 console.log(data)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月20日 21:11:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定