如何使用JavaScript减少和获取对象数组的值

3
我想要减少以下对象数组: arr=[{title: 'AP', CurrentDate: 2019-07-31, Status: 'done', NoOfEvents:'6' }] 现在我想要缩小这个数组对象以获取NoOfEvents值。
所以我尝试下面的代码但它不起作用: arr.reduce((n,x) => n + (x.title === 'AP' && new Date(x.CurrentDate) > (2019-05-15) + x.NoOfEvents,null) 如何在reduce中基于上述条件获得NoOfEvents的值?

如果您只需要基于条件获取“NoOfEvents”值,为什么要使用reduce呢? - aravind_reddy
你得到的结果是什么? - Mukesh Kumar
1个回答

1

reduce()函数编写错误。您可以尝试以下方法。

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)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接