-2

Given the following Dates:

var b = [
    {Date:'Jan 2016', steel:10.98},
    {Date:'Feb 2016', steel:5.67},
    {Date:'Jan 2016', steel:3.14},
    {Date:'Feb 2016', steel:2.14},
    {Date:'Mar 2016', steel:12.14}
]

I want to calculate both sum and cumulative average with respect to the dates shown above.

My desired output is:

Sum ->

[{"Date": "Jan 2016","val": 14.12},
{"date": "Feb 2016","val": 7.81},
{"date": "Mar 2016","val": 12.14}]

Cumulative Average ->

[{"Date": "Jan 2016","val": 7.06 },
{"date": "Feb 2016","val": 5.48},
{"date": "Mar 2016","val": 6.814}]
3

1 Answer 1

0

const sumSteelAndConcrete = (s = { steel: 0 }, c = { steel: 1 }) => s.steel + c.steel; const findByDate = (arr, date) => arr.find(item => item.Date === date);

const result = dates.map( sumSteelAndConcrete( findByDate(steelDays, date), findByDate(steelDays, date) ) ) );

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

1 Comment

What is this? The answer? if is this your try code, add it in an edit to the question, not as an aswer

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.