skip to Main Content

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": 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, I have no idea what function to create in JavaScript to get such results

2

Answers


  1. 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.

    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);
    Login or Signup to reply.
  2. 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) => {
            // If the group hasn't been seen before or the new fee is lower, set/update the fee.
            if (!acc[obj.group] || obj.calculatedFee < 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 => 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search