-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.

1
  • 1
    data is a simple array, so all you need to do is prepend a new element … unshift is the method to do that. Commented Mar 6, 2017 at 12:08

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

@CBroe is right, It's a duplicate of stackoverflow.com/questions/10773813/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.