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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With