Find all array keys which includes 'id' as prefix and rename them as just 'id'

32 Views Asked by At

I need to find the key names on an array that includes 'id' as prefix and change them as 'id' as unique key. There will be more elements added from back-end to the array with id key names with many variations as possible.

  1. First find key names that include 'id'.
  2. Delete string that doesn't match with 'id' prefix.

Initial array:

const data = [
    {
        "name": "Name 01",
        "description": "Name 02",
        "createdDate": "",
        "modifiedDate": null,
        "status": false,
        "idMapType": 1,
    },
    {
        "name": "Name 01",
        "description": "Name 02",
        "createdDate": "",
        "modifiedDate": null,
        "status": true,
        "idProjectStatus": 2, 
    }
]

Desired output:

const data = [
    {
        "name": "Name 01",
        "description": "Name 02",
        "createdDate": "",
        "modifiedDate": null,
        "status": false,
        "id": 1,
    },
    {
        "name": "Name 01",
        "description": "Name 02",
        "createdDate": "",
        "modifiedDate": null,
        "status": true,
        "id": 2, 
    }
]
0

There are 0 best solutions below