【问题标题】:How to reduce and get value from object array using javascript如何使用javascript减少对象数组并从对象数组中获取值
【发布时间】:2019-12-08 18:43:37
【问题描述】:

我想减少对象数组,如下所示: 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
  • 你得到什么结果..?

标签: javascript arrays reactjs


【解决方案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)

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 2021-09-27
    • 2019-03-31
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多