寻找分组的最小值

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

Finding minimum value by 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": 2, "calculatedFee": 10, "feeNumber": 10, "group": "3" },
  { "bankNumber": 2, "calculatedFee": 100, "feeNumber": 10, "group": "3" },
  { "bankNumber": 3, "calculatedFee": 100, "feeNumber": 10, "group": "2" },
]

我想要过滤这些数据,以获取具有相同 group 的最小值 calculatedFee

例如,对于 group1calculatedFee = 0 是最小值,对于 group3calculatedFee = 10 是最小值。然而,在JavaScript中应该创建什么函数来获得这样的结果?

英文:

I have an 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": 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.

For example, calculatedFee = 0 is the smallest for group 1, calculatedFee = 10 is the smallest for group 3. However, what function should I create in JavaScript to get such results?

答案1

得分: 3

您可以在每次迭代中使用标准的“按组”检查,检查当前的 calculatedFee 是否小于先前存储的组的值,或者如果该组尚未存储则为 Infinity

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: 2, calculatedFee: 10, feeNumber: 10, group: "3", }, { bankNumber: 2, calculatedFee: 100, feeNumber: 10, group: "3", }, { bankNumber: 3, calculatedFee: 100, feeNumber: 10, group: "2", },];

const minFeesByGroup = {};

for (const datum of data) {
  if (datum.calculatedFee < (minFeesByGroup[datum.group] ?? Infinity)) {
    minFeesByGroup[datum.group] = datum.calculatedFee;
  }
}

console.log(minFeesByGroup);

希望这对您有帮助!

英文:

You can use a standard 'group-by' checking on each iteration if the current calculatedFee is less than the previously stored value for the group or Infinity if the group isn't stored yet.

<!-- 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: 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 minFeesByGroup = {};

for (const datum of data) {
  if (datum.calculatedFee &lt; (minFeesByGroup[datum.group] ?? Infinity)) {
    minFeesByGroup[datum.group] = datum.calculatedFee;
  }
}

console.log(minFeesByGroup);

<!-- end snippet -->

答案2

得分: -3

你可以通过 JavaScript 中的 Array.prototype.reduce 和 Array.prototype.filter 的结合来实现这一点。

以下是执行此任务的函数:

function getSmallestFeeByGroup(data) {
    // 最初,找到每个组的最小费用。
    const minFeesByGroup = data.reduce((acc, obj) => {
        // 如果之前未见过该组或新费用更低,则设置/更新费用。
        if (!acc[obj.group] || obj.calculatedFee < acc[obj.group]) {
            acc[obj.group] = obj.calculatedFee;
        }
        return acc;
    }, {});

    // 然后,过滤原始数据,只包括费用与其组的最小费用相匹配的对象。
    return data.filter(obj => obj.calculatedFee === minFeesByGroup[obj.group]);
}

// 您的数据数组。
const data = [/*...*/];

console.log(getSmallestFeeByGroup(data));

这个函数首先计算每个组的最小 calculatedFee,并将它们存储在 minFeesByGroup 对象中。然后它过滤原始数组,只包括那些 calculatedFee 与其组的最小费用相匹配的对象。

英文:

You can accomplish this through the union of Array.prototype.reduce and Array.prototype.filter in JavaScript.

Here's a function that will carry out the task:

function getSmallestFeeByGroup(data) {
    // Initially, discover the minimum fee for each group.
    const minFeesByGroup = data.reduce((acc, obj) =&gt; {
        // If the group hasn&#39;t been seen before or the new fee is lower, set/update the fee.
        if (!acc[obj.group] || obj.calculatedFee &lt; acc[obj.group]) {
            acc[obj.group] = obj.calculatedFee;
        }
        return acc;
    }, {});

    // Afterwards, filter the original data to only comprise the objects where the fee matches the minimum for its group.
    return data.filter(obj =&gt; obj.calculatedFee === minFeesByGroup[obj.group]);
}

// Your data array.
const data = [/*...*/];

console.log(getSmallestFeeByGroup(data));

This function initially calculates the minimum calculatedFee for each group and stores them within the minFeesByGroup object. Then it filters the original array to solely incorporate those objects where the calculatedFee coincides with the minimum for its group.

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

发表评论

匿名网友

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

确定