Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce and get value from object array using javascript

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?

like image 444
Snehal Ramteke Avatar asked Jul 29 '26 11:07

Snehal Ramteke


1 Answers

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)
like image 67
MH2K9 Avatar answered Jul 30 '26 23:07

MH2K9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!