-1

In my project I make a request to the api database and get all my posts I then filter them by their stage value and create a new array for each stage. But I also want to loop through each stage array (opportunityPosts, prospectPosts, proposalPosts) and get the total cost value. I know this can be done with reduce() but if I try call reduce on any of these arrays I get an error because the array is not yet defined.

So how can I get the total cost of each stage?

Here's some demo code that I made:

let posts = [
    {
        "stage": "Opportunity",
        "cost": 1200
    },
    {
        "stage": "Prospect",
        "cost": 1500
    },
    {
        "stage": "Opportunity",
        "cost": 10000
    },
    {
        "stage": "Proposal",
        "cost": 1900
    },
    {
        "stage": "Prospect",
        "cost": 500
    }
];

let opportunityPosts = posts.filter(post => {
    if( post.stage == 'Opportunity' ){
        return post;
    }
});

let prospectPosts = posts.filter(post => {
    if( post.stage == 'Prospect' ){
        return post;
    }
});

let proposalPosts = posts.filter(post => {
    if( post.stage == 'Proposal' ){
        return post;
    }
});

console.log(opportunityPosts);
console.log(prospectPosts);
console.log(proposalPosts);
Reece
  • 2,581
  • 10
  • 42
  • 90
  • 1
    The function passed to `.filter()` should return a boolean. – Andreas Mar 26 '20 at 14:45
  • 1
    "*if I try call reduce on any of these arrays I get an error because the array is not yet defined.*" can you clarify how are you calling `reduce`? – VLAZ Mar 26 '20 at 14:46
  • @Andreas so what function should I use instead of .filter() – Reece Mar 26 '20 at 14:46
  • Imho, this is a dupe of: [how to group by and sum array of object?](https://stackoverflow.com/questions/29364262/how-to-group-by-and-sum-array-of-object) – Andreas Mar 26 '20 at 14:53
  • Does this answer your question? [Sum javascript object propertyA values with same object propertyB in array of objects](https://stackoverflow.com/questions/19233283/sum-javascript-object-propertya-values-with-same-object-propertyb-in-array-of-ob) – VLAZ Mar 26 '20 at 14:56

1 Answers1

1

So how can I get the total cost of each stage?

You can use array reduce() method to get total like:

let posts=[{stage:"Opportunity",cost:1200},{stage:"Prospect",cost:1500},{stage:"Opportunity",cost:1e4},{stage:"Proposal",cost:1900},{stage:"Prospect",cost:500}];
const sum = (arr, key) => arr.reduce((a, b) => a + (b[key] || 0), 0);

// Get only Opportunity posts
let opportunityPosts = posts.filter(post => post.stage == 'Opportunity');

// Get only Opportunity posts total
let opportunityTotal = sum(opportunityPosts, 'cost');

console.log('OpportunityPostsTotal: ', opportunityTotal)

// Do the same for other stages like:
let prospectPosts = posts.filter(post => post.stage == 'Prospect');
let proposalPosts = posts.filter(post => post.stage == 'Proposal');

let prospectTotal = sum(prospectPosts, 'cost');
let proposalTotal = sum(proposalPosts, 'cost');

console.log('ProspectPostsTotal: ', prospectTotal)
console.log('ProposalPostsTotal: ', proposalTotal)
palaѕн
  • 72,112
  • 17
  • 116
  • 136