Search code examples
javascriptreactjsant-design-pro

how to add new key value pair from frontend in API response


how would i add a new key value pair from frontend in API response

e.g

I have the following response :

[
  {
    "userId": 1,
    "id": 3,
    "title": "fugiat veniam minus",
  },
  {
    "userId": 1,
    "id": 4,
    "title": "et porro tempora",
  },
]

and I want to add this key value pair ("completed": false) to make it look like the following response:

[ 
  {
    "userId": 1,
    "id": 3,
    "title": "fugiat veniam minus",
    "completed": false
  },
  {
    "userId": 1,
    "id": 4,
    "title": "et porro tempora",
    "completed": true
  },
]

Solution

  • You can do this with map. and set true, false with the desired condition.

    const newArr = [
      {
        "userId": 1,
        "id": 3,
        "title": "fugiat veniam minus",
      },
      {
        "userId": 1,
        "id": 4,
        "title": "et porro tempora",
      }
    ].map(v => ({...v, completed: true or false}))