# Modifying Array inside Object Type Payload

**URL:** <https://discourse.nodered.org/t/modifying-array-inside-object-type-payload/54237>\
**Category:** General\
**Tags:** function-node\
**Created:** [25 November 2021 01:42 UTC](https://discourse.nodered.org/t/modifying-array-inside-object-type-payload/54237 "2021-11-25T01:42:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sheminasalam](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sheminasalam/32/8981_2.png) [@sheminasalam](https://discourse.nodered.org/u/sheminasalam)\
**Post date:** [25 November 2021 01:42 UTC](https://discourse.nodered.org/t/modifying-array-inside-object-type-payload/54237/1 "2021-11-25T01:42:43Z")

</div>

The below payload has 4 array inside data. One of these is `refresh_tokens` and in it one of the properties is `last_used` which has a datetime in it. I want to use this datatime to know if 3 days has past from that date and then delete the segment of array with `last_used` beyond 3 days.

```auto
{
  "version": 1,
  "key": "auth",
  "data": {
    "users": [
      {
        "id": 456,
        "is_active": true,
        "system_generated": false
      }
    ],
    "groups": [
      {
        "id": "admin",
        "name": "deff12"
      }
    ],
    "credentials": [
      {
        "id": 45,
        "auth": null,
        "data": {
          "username": "user"
        }
      }
    ],
    "refresh_tokens": [
      {
        "user_id": 122,
        "last_used_at": null,
        "version": null
      },
      {
        "user_id": 123,
        "last_used_at": "2021-11-25T00:54:31.341554+00:00",
        "version": null
      },
      {
        "user_id": 134,
        "last_used_at": "2021-11-21T01:19:56.970797+00:00",
        "version": "2021.11.4"
      }
    ]
  }
}

```

For example in the above payload, the 3 segment in `refresh_tokens` has the `last_used` datetime as `2021-11-21T01:19:56.970797+00:00`. More than 3 days has passed since it and so deleting it the final payload should look like below.

```auto
{
  "version": 1,
  "key": "auth",
  "data": {
    "users": [
      {
        "id": 456,
        "is_active": true,
        "system_generated": false
      }
    ],
    "groups": [
      {
        "id": "admin",
        "name": "deff12"
      }
    ],
    "credentials": [
      {
        "id": 45,
        "auth": null,
        "data": {
          "username": "user"
        }
      }
    ],
    "refresh_tokens": [
      {
        "user_id": 122,
        "last_used_at": null,
        "version": null
      },
      {
        "user_id": 123,
        "last_used_at": "2021-11-25T00:54:31.341554+00:00",
        "version": null
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [25 November 2021 07:31 UTC](https://discourse.nodered.org/t/modifying-array-inside-object-type-payload/54237/2 "2021-11-25T07:31:50Z")

</div>

Use javascript's filter() to filter out based on the conditions

```auto
// @ts-nocheck
function daysSince(date) {

    let today = new Date();
    let checkDay = new Date(date);

    let difference = Math.abs(checkDay - today);
    let days = difference / (1000 * 3600 * 24)

    return days
}

// filter tokens
msg.payload.data.refresh_tokens = msg.payload.data.refresh_tokens.filter(token => {
    if (token.last_used_at && token.last_used_at !== null && (daysSince(token.last_used_at) < 3)) {
        return true
    }

})

return msg;

```

double check if the conditions are as you need because i didnt test it extensively

```auto
[{"id":"9021f15dce7b58b4","type":"function","z":"5847b7aa62131d37","name":"","func":"// @ts-nocheck\nfunction daysSince(date) {\n\n let today = new Date();\n let checkDay = new Date(date);\n\n let difference = Math.abs(checkDay - today);\n let days = difference / (1000 * 3600 * 24)\n\n return days\n}\n\n// filter tokens\nmsg.payload.data.refresh_tokens = msg.payload.data.refresh_tokens.filter(token => {\n if (token.last_used_at && token.last_used_at !== null && (daysSince(token.last_used_at) < 3)) {\n return true\n }\n\n})\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":1160,"wires":[["836b64b27be9a64e"]]},{"id":"401681b625dd9834","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"version\":1,\"key\":\"auth\",\"data\":{\"users\":[{\"id\":456,\"is_active\":true,\"system_generated\":false}],\"groups\":[{\"id\":\"admin\",\"name\":\"deff12\"}],\"credentials\":[{\"id\":45,\"auth\":null,\"data\":{\"username\":\"user\"}}],\"refresh_tokens\":[{\"user_id\":122,\"last_used_at\":null,\"version\":null},{\"user_id\":123,\"last_used_at\":\"2021-11-25T00:54:31.341554+00:00\",\"version\":null},{\"user_id\":134,\"last_used_at\":\"2021-11-21T01:19:56.970797+00:00\",\"version\":\"2021.11.4\"}]}}","payloadType":"json","x":260,"y":1160,"wires":[["ed259ce6ad8e5012","9021f15dce7b58b4"]]},{"id":"ed259ce6ad8e5012","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":1080,"wires":[]},{"id":"836b64b27be9a64e","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":1160,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![sheminasalam](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sheminasalam/32/8981_2.png) [@sheminasalam](https://discourse.nodered.org/u/sheminasalam)\
**Post date:** [25 November 2021 08:02 UTC](https://discourse.nodered.org/t/modifying-array-inside-object-type-payload/54237/3 "2021-11-25T08:02:01Z")

</div>

Thanks mate

---

<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:** [9 December 2021 08:02 UTC](https://discourse.nodered.org/t/modifying-array-inside-object-type-payload/54237/4 "2021-12-09T08:02:30Z")

</div>

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