0

i want to calculate the total costs of products selected by a user under each company.There is my code.

let companiesProductPrices = [];  
               let prices = [];   
               this.selectedCompaniesDetails.forEach(company => {
                   this.chosenItems.forEach(item=> {
                      item.product_attributes.forEach(product => {
                                        if(company.id == product.company_id)
                                            {
                                                 prices.push(product.price);
                                                companiesProductPrices[company.id] = prices;
                                            }                                   
                                    })
                   });
                })

Background information. I have to objects. The products object and the companies object.

Products object referred to as this.chosenItems in my code and is like this;

[
  {
    "id": 1,
    "name": "Zimgold",
    "slug": null,
    "product_category_id": 1,
    "industry_id": 1,
    "category": {
      "id": 1,
      "name": "Cooking Oil",
      "slug": null,
      "industry_id": 1
    },
    "product_attributes": [
      {
        "id": 1,
        "description": "2 litres",
        "discount": null,
        "price": "120",
        "tax": null,
        "company_id": 1,
        "product_id": 1
      },
      {
        "id": 5,
        "description": "2 litres",
        "discount": null,
        "price": "130",
        "tax": null,
        "company_id": 2,
        "product_id": 1
      }
    ]
  },
  {
    "id": 4,
    "name": "Silo",
    "slug": null,
    "product_category_id": 3,
    "industry_id": 1,
    "category": {
      "id": 3,
      "name": "Mealie Meal",
      "slug": null,
      "industry_id": 1
    },
    "product_attributes": [
      {
        "id": 4,
        "description": "10 kgs",
        "discount": null,
        "price": "130",
        "tax": null,
        "company_id": 1,
        "product_id": 4
      }
    ]
  }
]

and my company object referred to as this.selectedCompaniesDetails in my code and is like this.

[
  {
    "id": 1,
    "name": "Spar",
    "slug": null,
    "industry_id": 1
  },
  {
    "id": 2,
    "name": "Ok",
    "slug": null,
    "industry_id": 1
  },
  {
    "id": 3,
    "name": "TM",
    "slug": null,
    "industry_id": 1
  },
  {
    "id": 4,
    "name": "choppies",
    "slug": null,
    "industry_id": 1
  },
  {
    "id": 5,
    "name": "Bon Marche",
    "slug": null,
    "industry_id": 1
  }
]

I'm wishing to get the totals of all products that are belong to each company. Let me show you my UI.enter image description here

So what i want is under spar the total should be 250 and under 130 and so forth for all other companies. Thank you in advance

1 Answer 1

1

This is one of many solutions. It's improvable, it's just to show you the way.

#1 Create an object where key is company id and value is the array of prices for that company

const priceByCompany = products.reduce((tot, prod) => {
   prod.product_attributes.forEach((p) => {
      if (!tot[p.company_id]) {
         tot[p.company_id] = []
      }
      const numberPrice = parseInt(p.price, 10)
      tot[p.company_id].push(numberPrice)
   })
   return tot
}, {})

// { 1: [120, 130], 2: [130] }

#2 Reduce all value array to sum them up.

Object.keys(priceByCompany).forEach((company) => {
    const total = priceByCompany[company].reduce((tot, price) => tot + price, 0)
    priceByCompany[company] = total
})

// { 1: 250, 2: 130 }

See the code in action: Jsfiddle

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

9 Comments

I'm getting the same response as with my code.
@BrianHighforceThomas Which? You didn't mentioned any error. Plus I've tested this code with the data object you provided and it worked.
I added a jsfiddle link to show you the result.
its not showing any errors but rather the result is not as i expect.Let me see the jsFiddle. Thanks
Did the jsfiddle worked for you? @BrianHighforceThomas :)
|

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.