简体   繁体   English

访问和更改2d Json数组,Javascript

[英]Access and change 2d Json Array, Javascript

I'm trying to access and change the data I receive from a JSON api call. 我正在尝试访问和更改从JSON API调用接收的数据。

The format of the call is like this: 呼叫的格式如下:

{
  "count": 391,
  "value": [
    {
      "id": "id1",
      "name": "name1",
      "url": "url1",
      "project": {
        "id": "projId",
        "name": "BT-GIT",
        "url": "otherurl",
        "state": "wellFormed"
      },
      "defaultBranch": "master",
      "remoteUrl": "remote"
    },
     {
      "id": "id2",
      "name": "name2",
      "url": "url2",
      "project": {
        "id": "projId",
        "name": "BT-GIT",
        "url": "otherurl",
        "state": "wellFormed"
      },
      "defaultBranch": "master",
      "remoteUrl": "remote"
    },...

and I want to add an extra entry to each "value", such that I have: 我想为每个“值”添加一个额外的条目,这样我就可以:

       {
          "id": "id1",
          "name": "name1",
          "url": "url1",
          "project": {
            "id": "projId",
            "name": "BT-GIT",
            "url": "otherurl",
            "state": "wellFormed"
          },
          "defaultBranch": "master",
          "remoteUrl": "remote"
          "date": "date" <---------
        }

I've tried: 我试过了:

$.each(data, function (idx) {
              data.value[idx].currentDate = new Date().toJSON().slice(0, 16);
          })

and: 和:

$.each(data, function (idx) {
              data[1][idx].currentDate = new Date().toJSON().slice(0, 16);
          })

any Ideas on how I could remedy this problem? 关于如何解决此问题的任何想法?

Much thanks. 非常感谢。

data.value = data.value.map(function(v) { v.date = Date.now() })

You can do with your existing code but just need slightly tweaking, 您可以使用现有代码,但只需稍作调整,

var data = {
    "count": 391,
    "value": [{
        "id": "id1",
        "name": "name1",
        "url": "url1",
        "project": {
            "id": "projId",
            "name": "BT-GIT",
            "url": "otherurl",
            "state": "wellFormed"
        },
        "defaultBranch": "master",
        "remoteUrl": "remote"
    }, {
        "id": "id2",
        "name": "name2",
        "url": "url2",
        "project": {
            "id": "projId",
            "name": "BT-GIT",
            "url": "otherurl",
            "state": "wellFormed"
        },
        "defaultBranch": "master",
        "remoteUrl": "remote"
    }]
};

$.each(data.value, function (index,data) {
              data.currentDate = new Date().toJSON().slice(0, 16);
          });
console.log(data);

See Demo : https://jsfiddle.net/mj3vu6s6/4/ 参见演示: https : //jsfiddle.net/mj3vu6s6/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM