【问题标题】:Filter array of objects where object property is date过滤对象属性为日期的对象数组
【发布时间】:2018-08-03 12:42:10
【问题描述】:

我有一个对象数组,每个对象看起来像这样

{
   "ActionDateTime": "2018-07-31T11:07:11Z",
   "ExternalID": 3962770,
   "GrandTotal": 707.5,
   "InternalID": 52858173,
   "ItemName": "Ricciolini Pasta",
}

我需要过滤这个数组(可能使用moment.js),使“ActionDateType”等于当前月份(moment().format('MMMM') //August)

这是我的代码:

var array = [];
      
array = data.filter(function (el) {
   var a = el.ActionDateTime
   return moment(a).format('MMMM') == moment().format('MMMM')
})
        
console.log(array)

【问题讨论】:

  • 一切皆有可能。试过吗?
  • @AliaksandrPitkevich 是对的!您至少应该尝试... 提示:这肯定需要一个时间范围。
  • @Salketer 是的,我尝试这样做,但它返回一个空数组
  • 如果您尝试过,请显示您的代码
  • 您只向我们展示了一条记录...所以以防万一,您展示的记录是从 7 月开始的,而我们现在是在 8 月,这可能就是您一无所获的原因。

标签: javascript arrays object momentjs


【解决方案1】:

您可以使用 JavaScripts Date 来做到这一点。

由于您的ActionDateTime 值是可解析的格式,您可以使用Date.parse 来获取时间戳。

let date = Date.parse ("2018-07-31T11:07:11Z")

要获取当前月份,您可以使用new Date().getMonth ()

也将它们结合起来,你会得到这样的东西。

let arr = [{
   "ActionDateTime": "2018-08-31T11:07:11Z", //<-- Note that i changed 07 to 08
   "ExternalID": 3962770,
   "GrandTotal": 707.5,
   "InternalID": 52858173,
   "ItemName": "Ricciolini Pasta",
}]


let tempDate 	 = new Date (); //Initialise a new Date
let currentMonth = tempDate.getMonth (); //Get the current month, from the untouched date.

let filtered = arr.filter (({ActionDateTime}) => 
tempDate.setTime (Date.parse (ActionDateTime)) && 
tempDate.getMonth () === currentMonth);
   
console.log (filtered)

正如 cmets 中所指出的:我更改了日期,以便过滤器在八月运行时返回一个条目。

【讨论】:

  • 是的,我们是在八月,您将当前日期与之比较的月份是七月
  • 尝试将 ActionDateTime 更改为 "2018-08-31T11:07:11Z"
  • 由于这是公认的答案,您可能需要进行编辑以使代码不返回空数组...这是 OP 遇到的主要问题。
  • @Salketer 感谢您指出这一点。我更新了答案,但是,这只适用于下个月。
【解决方案2】:

function timeTest() {
  let dateStr = '2018-07-31T11:07:11Z'
  let dateTs = new Date(dateStr)
  let ret = dateTs.getFullYear().toString() + '.' +
            (dateTs.getMonth() + 1).toString() + '.' +
            dateTs.getDate().toString()

  console.log(ret)
}

timeTest()

我认为你可以使用 javascript 的 api。

【讨论】:

  • 我认为不是这样,因为我需要在当前月份过滤包含一百个对象的数组
【解决方案3】:

我已经修改了 OP 的代码(这当然可以工作,但我需要删除 moment 依赖才能让它在这里工作)。

您所做的是正确的,但是您需要过滤当月的数据,而您的数据来自上个月。我添加了 3 个数据,上个月的 1 个,本月的 2 个,但去年的一个。注释掉了当年的退货声明,不确定是否需要,但我预计它只是被遗忘了。

var data = [
{
   "ActionDateTime": "2018-07-31T11:07:11Z",
   "ExternalID": 3962770,
   "GrandTotal": 707.5,
   "InternalID": 52858173,
   "ItemName": "Ricciolini Pasta",
},
{
   "ActionDateTime": "2018-08-02T11:07:11Z",
   "ExternalID": 3962770,
   "GrandTotal": 707.5,
   "InternalID": 52858173,
   "ItemName": "Ricciolini Pasta",
},
{
   "ActionDateTime": "2017-08-02T11:07:11Z",
   "ExternalID": 3962770,
   "GrandTotal": 707.5,
   "InternalID": 52858173,
   "ItemName": "Ricciolini Pasta",
}];

let array = [];
let curDate = new Date();
array = data.filter(function (el) {
   let a = new Date(el.ActionDateTime)
   //return a.getMonth() ==curDate.getMonth() && a.getYear() ==curDate.getYear()
   return a.getMonth() ==curDate.getMonth()
})
        
console.log(array)

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 2016-05-14
    • 2019-05-29
    • 2018-12-25
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多