# Remove JSON array items whose CreatedDate is not within last 5 minutes

**URL:** https://discourse.nodered.org/t/remove-json-array-items-whose-createddate-is-not-within-last-5-minutes/30657
**Category:** General
**Created:** [24 July 2020 15:00 UTC](https://discourse.nodered.org/t/remove-json-array-items-whose-createddate-is-not-within-last-5-minutes/30657 "2020-07-24T15:00:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Honer08](https://avatars.discourse-cdn.com/v4/letter/h/aeb1de/32.png) [@Honer08](https://discourse.nodered.org/u/Honer08)
#### Post date: [24 July 2020 15:00 UTC](https://discourse.nodered.org/t/remove-json-array-items-whose-createddate-is-not-within-last-5-minutes/30657/1 "2020-07-24T15:00:53Z")

</div>

I have a function ("get only the recent") to filter out JSON items from JSON array where CreatedDate was not created within the last 5 minutes.

OR to put it in another perspective.......

keep all JSON array items that have a CreatedDate within the last 5 minutes.

 ![Screen Shot 2020-07-24 at 8.05.02 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/c/5c3cb3893d34c9cc7b1ca03f19066fc76fde7eff.png) ![Screen Shot 2020-07-24 at 8.05.13 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/540628b60ffffa35b947f7539fee557b06cc8d21.png)

I am just missing something and I can't seem to see it.

```auto
var now = new Date()
console.info("Now: " + now);
var fiveMinutesAgo = new Date()
fiveMinutesAgo.setMinutes(-5)

console.info("Filter by Date: " + fiveMinutesAgo);

var permitDateTime = new Date(el => el.DateCreated);

msg.payload = msg.payload.filter(el => el.DateCreated.includes(permitDateTime.getTime() < fiveMinutesAgo.getTime()))

return msg;

```

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [24 July 2020 15:08 UTC](https://discourse.nodered.org/t/remove-json-array-items-whose-createddate-is-not-within-last-5-minutes/30657/2 "2020-07-24T15:08:04Z")

</div>

Put some debugging in using node.warn(). Like  
`node.warn("permitDateTime"+permitDateTime)`  
`node.warn(el.DateCreated.includes(permitDateTime.getTime())`  
to get an idea of what the data actually is.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [24 July 2020 15:24 UTC](https://discourse.nodered.org/t/remove-json-array-items-whose-createddate-is-not-within-last-5-minutes/30657/3 "2020-07-24T15:24:58Z")

</div>

hmm, a few issues.

try this...

```auto
var now = new Date()
var fiveMinutesAgo = now.getTime() - (5*60000); //epoch minus 5 mins
msg.origPayload = msg.payload; //keep old one for reference / debugging
msg.fiveMinutesAgo = fiveMinutesAgo; //add ts it to msg so we can inspect it in a debug set to show complete message
msg.payload = msg.payload.filter(el => new Date(el.DateCreated).getTime() >= fiveMinutesAgo )

return msg;

```

EDIT...  
let me explain this line `msg.payload = msg.payload.filter(el => new Date(el.DateCreated).getTime() >= fiveMinutesAgo )`

I have no idea what the `type` of `el.DateCreated` is. It could be a string, it could be a date object - BUT - to get you going, I am taking no chances, so, I create a `new Date` from whatever it is, then get its epoch, then compare it as a number to the `fiveMinutesAgo` variable.

note also, I add a few other items to the `msg` object - this is so I can view / debug the whole operation in the debug sidebar (set your debug to show complete message)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [24 July 2020 15:34 UTC](https://discourse.nodered.org/t/remove-json-array-items-whose-createddate-is-not-within-last-5-minutes/30657/4 "2020-07-24T15:34:28Z")

</div>

I will say one more thing - where are you getting this data? from a database? If so, you _really_ should be doing this at the database side with a `WHERE` clause.

---

<div class="post-metadata">

### Author: ![Honer08](https://avatars.discourse-cdn.com/v4/letter/h/aeb1de/32.png) [@Honer08](https://discourse.nodered.org/u/Honer08)
#### Post date: [24 July 2020 18:43 UTC](https://discourse.nodered.org/t/remove-json-array-items-whose-createddate-is-not-within-last-5-minutes/30657/5 "2020-07-24T18:43:06Z")

</div>

Thank you! So very much!

---

<div class="post-metadata">

### Author: ![Honer08](https://avatars.discourse-cdn.com/v4/letter/h/aeb1de/32.png) [@Honer08](https://discourse.nodered.org/u/Honer08)
#### Post date: [24 July 2020 18:45 UTC](https://discourse.nodered.org/t/remove-json-array-items-whose-createddate-is-not-within-last-5-minutes/30657/6 "2020-07-24T18:45:26Z")

</div>

This data was retrieved from Wufoo, using the Wufoo http get API.  
But, I agree. Using a SQL WHERE clause would be much easier.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [7 August 2020 18:45 UTC](https://discourse.nodered.org/t/remove-json-array-items-whose-createddate-is-not-within-last-5-minutes/30657/7 "2020-08-07T18:45:27Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
