0

I'm trying to filter data with javascript for an object like this,

[{created: 1601820360, magic: -0.1, power: 0.1, createdDat: "2020-10-05"}
   {created: 1601820365, magic: -0.8, power: 0.8, createdDat: "2020-10-05"},..]

I want to filter the array by date, and i'm thinking of using the created field to avoid relying on two date fields.

I'm sort of confused about how to do this with UNIX timestamps - do I need to convert the timestamps or can I do this more simply? And how would I filter the data array (a simple for loop?)

Thanks in advance - bit lost on this one

Dan O
  • 6,022
  • 2
  • 32
  • 50
LeCoda
  • 538
  • 7
  • 36
  • 79
  • 1
    You have two questions which are both answered: [Filter arrays](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) and [Convert unix timestamp](https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript). Although, you can just use logical operators directly on the `created` field. – Chris Sep 01 '21 at 14:36
  • Does this answer your question? [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – Chris Sep 01 '21 at 14:40
  • Almost! Filtering part is sick, moreso trying to workout how to filter based on current unix timestamp of the day, back to say a month before that date – LeCoda Sep 01 '21 at 14:40

1 Answers1

1

you can use Date() and filter() :

myArray = [{created: 1601820360, magic: -0.1, power: 0.1, createdDat: "2020-10-05"},{created: 1601820365, magic: -0.8, power: 0.8, createdDat: "2020-10-05"}];

let filterDate = new Date('2020-11-04');
filterDate.setHours(0, 0, 0, 0);

// get 1 month ago
filterDate.setMonth(filterDate.getMonth() - 1);

// filter on date
let filteredArray = myArray.filter(entry => {
  let dateCreated = new Date(entry.created * 1000);
  dateCreated.setHours(0, 0, 0, 0);
  return dateCreated.getTime() == filterDate.getTime()
});

console.log(filteredArray);
techws
  • 108
  • 8
  • Thanks @techws . How would I do the same thing and filter by the current date, back to a month previously? – LeCoda Sep 01 '21 at 14:41
  • How do i do this, but have it take the current (most recent) date automatically, so not setting it – LeCoda Sep 01 '21 at 14:49
  • Date.now() and then setHours(0, 0, 0, 0) – techws Sep 01 '21 at 14:52
  • Mm the example isn't working with the unix datasets going in – LeCoda Sep 01 '21 at 14:53
  • 1
    this is because your created timestamp contains time, I edited code – techws Sep 01 '21 at 14:56
  • TypeError: filterDate.setHours is not a function - error i'm getting – LeCoda Sep 01 '21 at 15:08
  • I edited the example (tested on JSFiddle). There can be a problem with timezone conversion for the timestamp and the date you set. In your example the date is 2020-10-05 but timestamp are on 2020-10-04 GMT. If you want the current date for the filteredDate just set new Date() – techws Sep 02 '21 at 07:04