我正在尝试访问和更改从JSON api调用中收到的数据。
通话的格式如下:
{
"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"
},...
我希望为每个“值”添加一个额外的条目,这样我就有了:
{
"id": "id1",
"name": "name1",
"url": "url1",
"project": {
"id": "projId",
"name": "BT-GIT",
"url": "otherurl",
"state": "wellFormed"
},
"defaultBranch": "master",
"remoteUrl": "remote"
"date": "date" <---------
}
我试过了:
$.each(data, function (idx) {
data.value[idx].currentDate = new Date().toJSON().slice(0, 16);
})
和
$.each(data, function (idx) {
data[1][idx].currentDate = new Date().toJSON().slice(0, 16);
})
关于如何解决这个问题的任何想法?
非常感谢。
答案 0 :(得分:0)
data.value = data.value.map(function(v) { v.date = Date.now() })
答案 1 :(得分:0)
您可以使用现有代码,但只需稍加调整,
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);