3

I want to reduce object array which is as below: arr=[{title: 'AP', CurrentDate: 2019-07-31, Status: 'done', NoOfEvents:'6' }]

Now I want to reduce this array object to get NoOfEvents value.

So I am trying below code but it's not working:

arr.reduce((n,x) => n + (x.title === 'AP' && new Date(x.CurrentDate) > (2019-05-15) + x.NoOfEvents,null)

How do I get value of NoOfEvents based on above condition in reduce?

2
  • why do you want to use reduce if you only need NoOfEvents value based on condition Commented Jul 31, 2019 at 9:32
  • what is the result you are getting..? Commented Jul 31, 2019 at 9:35

1 Answer 1

1

The reduce() function wrote is wrongly formed. You can try the following way.

const arr = [{title: 'AP', CurrentDate: '2019-07-31', Status: 'done', NoOfEvents:'6' }]
const result = arr.reduce((n, x) => (n += (x.title === 'AP' && new Date(x.CurrentDate) > new Date('2019-05-15')) ? +x.NoOfEvents: 0, n), 0)
console.log(result)

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

Comments

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.