如何根据时间戳过滤对象数组

时间:2022-12-29 20:13:15

I have more than 1 thousand array of objects and every object inside the array contains date and time fileds. This records are inserted to DB for every seconds as shown below. Here I want get pas 7 days record including minutes and seconds based upon current date+hours+seconds.

我有超过一千个对象数组,数组中的每个对象都包含日期和时间fileds。此记录每秒都会插入DB,如下所示。在这里,我希望得到7天的记录,包括基于当前日期+小时+秒的分钟和秒。

Example:

[{
    "date": "11/25/2016 08:45:58",
    "energy": 29940913188,
    "power": 6783,
    "time": 217781102
}, {
    "date": "11/25/2016 08:46:01",
    "energy": 29940913267,
    "power": 6792,
    "time": 217781105
}, {
    "date": "11/25/2016 08:46:02",
    "energy": 29940913318,
    "power": 6791,
    "time": 217781107
}, {
    "date": "11/25/2016 08:46:04",
    "energy": 29940913344,
    "power": 6797,
    "time": 217781108
}, {
    "date": "11/25/2016 08:46:05",
    "energy": 29940913396,
    "power": 6816,
    "time": 217781110
}, {
    "date": "11/25/2016 08:46:07",
    "energy": 29940913421,
    "power": 6798,
    "time": 217781111
}, {
    "date": "11/25/2016 08:46:08",
    "energy": 29940913473,
    "power": 6804,
    "time": 217781113
}]

Please let me know how can I do this by using JS or jquery

请告诉我如何使用JS或jquery来完成此操作

2 个解决方案

#1


2  

You can use the filter as below to achieve this.

您可以使用以下过滤器来实现此目的。

data.filter((d) => {
  return new Date(d.date).getTime() >= seventhDay.getTime();
});

Here, if your date data type is Date already, you don't need to convert to date again new Date(d.date).

在这里,如果您的日期数据类型已经是日期,则无需再次将日期转换为日期新日期(d.date)。

Also, it will work without getTime() as below.

此外,它将在没有getTime()的情况下工作,如下所示。

return new Date(d.date) >= seventhDay;

var data = [{
    "date": "11/20/2016 08:45:58",
    "energy": 29940913188,
    "power": 6783,
    "time": 217781102
}, {
    "date": "11/25/2016 08:46:01",
    "energy": 29940913267,
    "power": 6792,
    "time": 217781105
}, {
    "date": "11/25/2016 08:46:02",
    "energy": 29940913318,
    "power": 6791,
    "time": 217781107
}, {
    "date": "11/25/2016 08:46:04",
    "energy": 29940913344,
    "power": 6797,
    "time": 217781108
}, {
    "date": "11/25/2016 08:46:05",
    "energy": 29940913396,
    "power": 6816,
    "time": 217781110
}, {
    "date": "11/25/2016 08:46:07",
    "energy": 29940913421,
    "power": 6798,
    "time": 217781111
}, {
    "date": "11/21/2016 08:46:08",
    "energy": 29940913473,
    "power": 6804,
    "time": 217781113
}];

var seventhDay = new Date();
seventhDay.setDate(seventhDay.getDate() - 7);


var filteredData = data.filter((d) => {
  return new Date(d.date).getTime() >= seventhDay.getTime();
});

console.log(filteredData);

#2


0  

You could so something like this.

你可以这样的事情。

var input = [{
  "date": "11/22/2016 08:45:58",
  "energy": 29940913188,
  "power": 6783,
  "time": 217781102
}, {
  "date": "11/25/2016 08:46:01",
  "energy": 29940913267,
  "power": 6792,
  "time": 217781105
}, {
  "date": "11/25/2016 08:46:02",
  "energy": 29940913318,
  "power": 6791,
  "time": 217781107
}, {
  "date": "11/25/2016 08:46:04",
  "energy": 29940913344,
  "power": 6797,
  "time": 217781108
}, {
  "date": "11/25/2016 08:46:05",
  "energy": 29940913396,
  "power": 6816,
  "time": 217781110
}, {
  "date": "11/25/2016 08:46:07",
  "energy": 29940913421,
  "power": 6798,
  "time": 217781111
}, {
  "date": "11/25/2016 08:46:08",
  "energy": 29940913473,
  "power": 6804,
  "time": 217781113
}];

function filter(format) {
  return input.filter((obj) => {
    return obj.date.indexOf(format) !== -1;
  });
}

var last7Days = getLast7Days();
var results = last7Days.map((date) => {
  return filter(date);
}).filter((result) => {
  return result.length != 0
});

console.log(...results);


function pad(number) {
  if (number < 10) {
    return '0' + number;
  }
  return number;
}

function getLast7Days() {
  var output = [];
  for (var idx = 0; idx < 7; idx++) {

    var date = new Date();
    var last = new Date(date.getTime() - ((idx + 1) * 24 * 60 * 60 * 1000));
    var day = pad(last.getDate());
    var month = pad(last.getMonth() + 1);
    var year = last.getFullYear();

    output.push(month + "/" + day + "/" + year);
  }
  console.log(output);
  return output;
}

#1


2  

You can use the filter as below to achieve this.

您可以使用以下过滤器来实现此目的。

data.filter((d) => {
  return new Date(d.date).getTime() >= seventhDay.getTime();
});

Here, if your date data type is Date already, you don't need to convert to date again new Date(d.date).

在这里,如果您的日期数据类型已经是日期,则无需再次将日期转换为日期新日期(d.date)。

Also, it will work without getTime() as below.

此外,它将在没有getTime()的情况下工作,如下所示。

return new Date(d.date) >= seventhDay;

var data = [{
    "date": "11/20/2016 08:45:58",
    "energy": 29940913188,
    "power": 6783,
    "time": 217781102
}, {
    "date": "11/25/2016 08:46:01",
    "energy": 29940913267,
    "power": 6792,
    "time": 217781105
}, {
    "date": "11/25/2016 08:46:02",
    "energy": 29940913318,
    "power": 6791,
    "time": 217781107
}, {
    "date": "11/25/2016 08:46:04",
    "energy": 29940913344,
    "power": 6797,
    "time": 217781108
}, {
    "date": "11/25/2016 08:46:05",
    "energy": 29940913396,
    "power": 6816,
    "time": 217781110
}, {
    "date": "11/25/2016 08:46:07",
    "energy": 29940913421,
    "power": 6798,
    "time": 217781111
}, {
    "date": "11/21/2016 08:46:08",
    "energy": 29940913473,
    "power": 6804,
    "time": 217781113
}];

var seventhDay = new Date();
seventhDay.setDate(seventhDay.getDate() - 7);


var filteredData = data.filter((d) => {
  return new Date(d.date).getTime() >= seventhDay.getTime();
});

console.log(filteredData);

#2


0  

You could so something like this.

你可以这样的事情。

var input = [{
  "date": "11/22/2016 08:45:58",
  "energy": 29940913188,
  "power": 6783,
  "time": 217781102
}, {
  "date": "11/25/2016 08:46:01",
  "energy": 29940913267,
  "power": 6792,
  "time": 217781105
}, {
  "date": "11/25/2016 08:46:02",
  "energy": 29940913318,
  "power": 6791,
  "time": 217781107
}, {
  "date": "11/25/2016 08:46:04",
  "energy": 29940913344,
  "power": 6797,
  "time": 217781108
}, {
  "date": "11/25/2016 08:46:05",
  "energy": 29940913396,
  "power": 6816,
  "time": 217781110
}, {
  "date": "11/25/2016 08:46:07",
  "energy": 29940913421,
  "power": 6798,
  "time": 217781111
}, {
  "date": "11/25/2016 08:46:08",
  "energy": 29940913473,
  "power": 6804,
  "time": 217781113
}];

function filter(format) {
  return input.filter((obj) => {
    return obj.date.indexOf(format) !== -1;
  });
}

var last7Days = getLast7Days();
var results = last7Days.map((date) => {
  return filter(date);
}).filter((result) => {
  return result.length != 0
});

console.log(...results);


function pad(number) {
  if (number < 10) {
    return '0' + number;
  }
  return number;
}

function getLast7Days() {
  var output = [];
  for (var idx = 0; idx < 7; idx++) {

    var date = new Date();
    var last = new Date(date.getTime() - ((idx + 1) * 24 * 60 * 60 * 1000));
    var day = pad(last.getDate());
    var month = pad(last.getMonth() + 1);
    var year = last.getFullYear();

    output.push(month + "/" + day + "/" + year);
  }
  console.log(output);
  return output;
}