简体   繁体   English

如何使用javascript从对象数组中减少和获取值

[英]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' }] 我想减少对象数组,如下所示: arr=[{title: 'AP', CurrentDate: 2019-07-31, Status: 'done', NoOfEvents:'6' }]

Now I want to reduce this array object to get NoOfEvents value. 现在我想减少这个数组对象以获得NoOfEvents值。

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? 如何在NoOfEvents基于上述条件获得NoOfEvents的值?

The reduce() function wrote is wrongly formed. 写入的reduce()函数编写错误。 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM