J'ai une API qui renvoie des données telles que celle-ci:
var groupBy = require('lodash.groupby'); var grouped = groupBy(data, function(x) { return x.Owner.Name; });
Celles-ci représentent des opportunités et chaque commercial en aura plusieurs. J'essaye de renvoyer un objet qui additionne les montants pour chaque vendeur et renvoie quelque chose comme:
{Steve Knight: 5590, Bob Smith: 98722, John Jones: 12347893}
J'ai essayé de regrouper les objets par nom de propriétaire mais je ne sais pas comment faire puis additionnez les montants
{ "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...
3 Réponses :
Vous pouvez essayer ceci ci-dessous:
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; } });
Il est préférable d'utiliser réduire
la méthode de 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) }());
Si le nom de la clé existe, accumulez la valeur déjà existante dans le nouveau, sinon ajoutez simplement la valeur initiale et le nom
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)
Y a-t-il une raison pour laquelle vous écrivez acc = {... acc, [key]: amount}
au lieu de simplement acc [key] = amount
?
@rplantiko vous avez raison, je viens de l'utiliser à cause de la mutation de l'état parfois, cela n'a pas d'impact sur les performances de toute façon et le ramasse-miettes fait assez bien son travail
Par état
, vous entendez état
Jetez un œil à ceci: stackoverflow.com/questions/14446511/...