-1

I am getting this object as response object

{
"data": [
    {
        "id": "203",
        "bench": "abc"
    },
    {
        "id": "205",
        "bench": "def"
    }
],
"responseCode": 200,
"isSuccess": "true"
}

now I want to add this object on top of the response object

{
  "id": "0",
  "bench": "Select bench"
}

so the final object should look like this

{
"data": [
    {
        "id": "0",
        "bench": "Select bench"
    },
    {
        "id": "203",
        "bench": "abc"
    },
    {
        "id": "205",
        "bench": "def"
    }
],
"responseCode": 200,
"isSuccess": "true"
}

is there any way to do it in typescript? I tried 'unshift' but it's not working.

sumit
  • 19
  • 1
  • 8
  • 1
    `data` is a simple array, so all you need to do is prepend a new element … `unshift` is the method to do that. – CBroe Mar 06 '17 at 12:08

1 Answers1

0
  response = {
    "data": [
    {
      "id": "203",
      "bench": "abc"
    },
    {
      "id": "205",
      "bench": "def"
    }
    ],
    "responseCode": 200,
    "isSuccess": "true"
  };

  response.data.unshift({
    "id": "0",
    "bench": "Select bench"
  });
  console.log(response); // shows json object
RKJ
  • 51
  • 6
  • @CBroe is right, It's a duplicate of http://stackoverflow.com/questions/10773813/adding-something-to-the-top-of-a-json-object – RKJ Mar 06 '17 at 12:20