0

Refer here: Calculating 2 object keys/columns from array of objects Javascript

Example:

var foodRevenue = {
      "Month": "2011-01",
      "week1_Revenue": "100.00",
      "week2_Revenue": "300.51",
      "Week3_Reenue": "200.09",
      "Month1_TotalRevenue": "0"
    },
    {Month, Month1_TotalRevenue, week1_Revenue,...revenue} = foodRevenue,
    result = {
      Month,
      week1_Revenue,
      Month1_TotalRevenue: 
        Object
          .values({...revenue,week1_Revenue})
          .reduce((s,r) => s+=+r, +Month1_TotalRevenue)
          .toFixed(2)
    }

console.log(result)

I want to swap week1_revenue and month1_TotalRevenue order from the calculated object key. How would I do that. I recommend using these solutions.

Note: The original array was a string, so I JSON.parse() so the array foodRevenue is already an object

9
  • @52d6c6af That's not JSON. foodRevenue and result are objects. Commented Mar 11, 2020 at 17:44
  • What I meant was that the original array that was parsed is not included. foodRevenue is the parsed object Commented Mar 11, 2020 at 17:45
  • {Month, Month1_TotalRevenue, week1_Revenue,...revenue} = foodRevenue is destructuring. Hop over to MDN and have a look at their documentation on that topic. Commented Mar 11, 2020 at 17:48
  • Clarification of what you want to achieve might help you get better answers. Commented Mar 11, 2020 at 18:14
  • The goal is to re-arrange the order of the result.My desire order is Month, week1_revenue, and Month1_TotalRevenue. The var foodRevenue is an object,so I am trying to use this solution to re-arrange the order. Commented Mar 11, 2020 at 18:27

0

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.