如何在javascript

时间:2017-03-06 12:05:50

标签: javascript json object typescript

我将此对象作为响应对象

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

现在我想在响应对象

之上添加此对象
{
  "id": "0",
  "bench": "Select bench"
}

所以最终的对象应该是这样的

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

有没有办法在打字稿中做到这一点?我试过'不合时宜',但它没有用。

1 个答案:

答案 0 :(得分: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