1

I have an API that returns data such as this:

{
        "attributes": {
            "type": "Opportunity"
        },
        "Amount": 175.36,
        "Owner": {
            "attributes": {
                "type": "User"
            },
            "Name": "Steve Knight"
        }
    },

    {
        "attributes": {
            "type": "Opportunity"
        },
        "Amount": 6800,
        "Owner": {
            "attributes": {
                "type": "User"
            },
            "Name": "Bob Smith"
        }
    }
etc...

These represent opportunities and so each salesperson will have multiple. I am trying to return an object that sums the amounts for each salesperson and returns something like:

{Steve Knight: 5590, Bob Smith: 98722, John Jones: 12347893}

I have tried grouping the objects by owner name however I am not sure how to then sum the amounts

var groupBy = require('lodash.groupby');

var grouped = groupBy(data, function(x) {
        return x.Owner.Name;
      });
1

3 Answers 3

2

Best to use the reduce method of Array.prototype

console.clear();

(function() {
  "use strict";

  function reduce(coll, elem, idx, arr) {
    coll[elem.Owner.Name] = coll[elem.Owner.Name] || 0;
    coll[elem.Owner.Name] += elem.Amount

    return coll;
  }

  const data = [
    {
      attributes: {
        type: "Opportunity"
      },
      Amount: 175.36,
      Owner: {
        attributes: {
          type: "User"
        },
        Name: "Steve Knight"
      }
    },

    {
      attributes: {
        type: "Opportunity"
      },
      Amount: 100.16,
      Owner: {
        attributes: {
          type: "User"
        },
        Name: "John Doe"
      }
    },

    {
      attributes: {
        type: "Opportunity"
      },
      Amount: 6.00,
      Owner: {
        attributes: {
          type: "User"
        },
        Name: "John Doe"
      }
    },

    {
      attributes: {
        type: "Opportunity"
      },
      Amount: 101.65,
      Owner: {
        attributes: {
          type: "User"
        },
        Name: "Steve Knight"
      }
    },

    {
      attributes: {
        type: "Opportunity"
      },
      Amount: 6800,
      Owner: {
        attributes: {
          type: "User"
        },
        Name: "Bob Smith"
      }
    }
  ];

  const reducedData = data.reduce(reduce, {})

  console.log(reducedData)
}());

Sign up to request clarification or add additional context in comments.

Comments

1

If key name exists than accumulate the already existing value to the new one, otherwise just add the initial value and name

const arr = [{
    "attributes": {
      "type": "Opportunity"
    },
    "Amount": 175.36,
    "Owner": {
      "attributes": {
        "type": "User"
      },
      "Name": "Steve Knight"
    }
  },
  {
    "attributes": {
      "type": "Opportunity"
    },
    "Amount": 100,
    "Owner": {
      "attributes": {
        "type": "User"
      },
      "Name": "Steve Knight"
    }
  },

  {
    "attributes": {
      "type": "Opportunity"
    },
    "Amount": 6800,
    "Owner": {
      "attributes": {
        "type": "User"
      },
      "Name": "Bob Smith"
    }
  }
]


const result = arr.reduce((acc, x) => {
  const key = x.Owner.Name;
  const amount = x['Amount'];

  if (!acc[key]) {
    acc[key] = amount;
  } else {
    acc[key] = acc[key] + amount;
  }

  return acc;
}, {});
console.log(result)

3 Comments

Is there a reason why you write acc = { ...acc, [key]:amount} instead of simply acc[key] = amount ?
@rplantiko you are right, just got used it because of mutating the sate sometimes, it doesn't impact the performance anyways and garbage collector does it's job pretty well
By sate you mean state
0

You can try this below:

let output = {}
data.map(function (item) {
    if (output.hasOwnProperty(item.Owner.Name)) {
            output[item.Owner.Name] = item.Amount;
    } else {
            output[item.Owner.Name] += item.Amount;
    }
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.